首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

一个关于文件下传的页面.遇到异常.求大神指导指导

2012-11-08 
一个关于文件上传的页面.遇到错误.求大神指导指导HTML code!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0

一个关于文件上传的页面.遇到错误.求大神指导指导

HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>upload.</title><style type="text/css"><!--.STYLE2 {    font-family: "宋体";    font-weight: bold;}--></style></head><body><style type="text/css"><!--tr,td{font-size:10px}--></style><center>    <h1 class="STYLE2">upload your file</h1>    <p>    <table border="1" cellspacing="0" cellpadding="1" bordercolordark="#ffffff" bordercolorlight="#0000ff" width="400">    <form action="up_back.php" method="post" enctype="multipart/form-data">    <tr bgcolor="#ccccff">    <td>chose your file:</td>    <td><input type="file" name="upfile" size=32 /></td>    </tr>    <tr>    <td>describe:</td>    <td><input type="text" name="describe" size="42" /></td>    </tr>    <tr>    <td>owner:</td>    <td><input type="text" name="owner" size="42" /></td>    </tr>    <tr>    <td>submit:</td>    <td><center><input type="submit" value="submit" /></center></td>    </tr>    </form>    </table>    </p></center>        </body></html>

这个是用来前台的页面调用下面的php.
PHP code
<?phpif (!$_POST["upfile"]&&$_FILES["upfile"]["name"]==""){    echo "no file<p>";    echo "click<a href=\"up_forward.html\">there</a>return!";}else {    $filepath="upload/";    $name=$filepath.$_FILES["upload"]["name"];    while (file_exists($name))    {        $temp=explode(".", $name);        $name=$temp[0]."0".".".$temp[1];    }    if(move_uploaded_file($_FILES["upfile"]["tmp_name"], $name))    {        if ($_POST["owner"])        {            $owner=$_POST["owner"];        }        else         {            $owner="NULL";        }        if ($_POST["describe"])        {            $describe=$_POST["describe"];        }        else        {            $describe="NONE";        }        $time=date("y-m-d H:m:s");        $content=$_FILES["upload"]["name"]."||".$owner."||".$describe."||".$time."\n";        file_put_contents("record.dat", $content.FILE_APPEND);        echo "name:".$_FILES["upfile"]["name"];        echo "<p>";        echo "is upload successfull.<p>";        echo "click<a href=\"up_forward.html\">there</a>return!";    }    else     {        echo "upload error!<p>";        echo "fail!<p>";        echo "click<a href=\"index.php\">there</a>check!";    }}?>

问题就在于上传之后,提示文件上传成功,但是upload文件夹里面文件名是0的倍数,而且没有文件格式..这个是怎么回事呢?


提示的错误就是下面这些..
Notice: Undefined index: upfile in D:\phpnow\htdocs\b\up_back.php on line 2

Notice: Undefined index: upload in D:\phpnow\htdocs\b\up_back.php on line 10



Notice: Undefined offset: 1 in D:\phpnow\htdocs\b\up_back.php on line 14

Notice: Undefined index: upload in D:\phpnow\htdocs\b\up_back.php on line 35
name:10.jpg

[解决办法]
这一行 $name=$filepath.$_FILES["upload"]["name"];
第一个文件上传的name="file" , 没有"upload" 啊.
[解决办法]
1.
if (!$_POST["upfile"]&&$_FILES["upfile"]["name"]=="")
==>
if ($_FILES["upfile"]["name"]=="") //这样就能判断是否选择文件了

2.
$name=$filepath.$_FILES["upload"]["name"];
==》
$name=$filepath.$_FILES["upfile"]["name"];

3. 如果你的文件存在了。while循环不就成了死循环了吗,可以改为:
if(file_exists($name))
{
$temp=explode(".", $name);
$name=$temp[0]."0".".".$temp[1];
}
4. $content=$_FILES["upfile"]["name"]."||".$owner."||".$describe."||".$time."\n";

热点排行