注册 登录
编程论坛 PHP技术论坛

请教文件上传错误的原因

xufan 发布于 2010-05-09 21:02, 784 次点击
程序代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP文件上传</title>
<script type="text/javascript">
function checkForm()
{
    if (form1.userfile.value="")
    {
        alert("请选择要上传的文件。");
        form1.userfile.focus();
        return false;
    }
}
</script>
</head>

<body>
<?php
if (!isset($_POST['submit'])){?>


<form name="fomr1" action="#" method="post" enctype="multipart/form-data" onsubmit="return checkForm()">
<table width="396" height="108" border="1" cellpadding="0" cellspacing="0" rules="none">
  <tr>
    <td colspan="2" bgcolor="#999999">文件上传</td>
  </tr>
  <tr>
    <td width="97">选择文件:</td>
    <td width="319"><input name="userfile" type="file" id="userfile" /></td>
  </tr>
  <tr>
    <td><input name="MAX_FILE_SIZE" type="hidden" value="5242880" /></td>
    <td><input type="submit" name="submit" value="执行文件上传" />&nbsp;&nbsp;<input type="reset" name="reset" value="重置" /></td>
  </tr>
</table>

<?php }else {
$uploaddir="../uploads/";
if (!file_exists($uploaddir)) mkdir($uploaddir);
$uploadfile=$uploaddir.basename($_FILES["userfile"]["name"]);
if (move_uploaded_file($_FILES["userfile"]["tmp_name"],$uploadfile))
{
    printf("<b>上传文件信息</b>\n");
    printf("<ul>");
    printf("<li>文件名:  %S</li>\n",$_FILES["userfile"]["name"]);
    printf("<li>文件类型:  %S</li>\n",$_FILES["userfile"]["type"]);
    printf("<li>文件大小:  %S</li>\n",$_FILES["userfile"]["size"]);
    printf("<li>文件名:  %S</li>\n",$_FILES["userfile"]["name"]);
    printf("<p>文件上传成功</p>\n");
    printf("<a href=\"%s\">返回</a>\n",basename($PHP_SELF));
   
}
else
{
    printf("<p>文件未能上传成功</p>");
}
?>
</body>
</html>
错误提示:Parse error: syntax error, unexpected $end in F:\AppServ\www\MyServer\chap03.php on line 62
怎么改呢?在线等待师傅教我
4 回复
#2
cnenc2010-05-09 23:18
你不应该把 if 语句拆成两段.


更更不应该把 { } 拆开.
#3
xufan2010-05-10 12:45
回复 2楼 cnenc
那该如何改正呢?请教哦
#4
cnenc2010-05-10 14:59
不好意思, 明天忘记看了.
你是在最后面少了一个 "}", 导致失败
#5
cnenc2010-05-10 14:59
程序代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP文件上传</title>
<script type="text/javascript">
function checkForm()
{
    if (form1.userfile.value="")
    {
        alert("请选择要上传的文件。");
        form1.userfile.focus();
        return false;
    }
}
</script>
</head>
<body>
<?php
if (!isset($_POST['submit']))
{
    print <<<EOT
    <form name="fomr1" action="#" method="post" enctype="multipart/form-data" onsubmit="return checkForm()">
    <table width="396" height="108" border="1" cellpadding="0" cellspacing="0" rules="none">
      <tr>
        <td colspan="2" bgcolor="#999999">文件上传</td>
      </tr>
      <tr>
        <td width="97">选择文件:</td>
        <td width="319"><input name="userfile" type="file" id="userfile" /></td>
      </tr>
      <tr>
        <td><input name="MAX_FILE_SIZE" type="hidden" value="5242880" /></td>
        <td><input type="submit" name="submit" value="执行文件上传" />&nbsp;&nbsp;<input type="reset" name="reset" value="重置" /></td>
      </tr>
    </table>
    </form>
EOT;
}
else
{
    $uploaddir="../uploads/";
    if (!file_exists($uploaddir)) mkdir($uploaddir);
    $uploadfile=$uploaddir.basename($_FILES["userfile"]["name"]);
    if (move_uploaded_file($_FILES["userfile"]["tmp_name"],$uploadfile))
    {
        echo "<b>上传文件信息</b>\n";
        echo "<ul>";
        echo "<li>文件名:".$_FILES["userfile"]["name"] ."</li>\n" ;
        echo "<li>文件类型:".$_FILES["userfile"]["type"] ."</li>\n" ;
        echo "<li>文件大小:".$_FILES["userfile"]["size"] ."</li>\n" ;
        echo "<li>文件名:".$_FILES["userfile"]["name"] ."</li>\n" ;
        echo "<p>文件上传成功</p>\n" ;
        echo "<a href=\"".$_SERVER['SCRIPT_NAME']."\">返回</a>\n";
    }
    else
    {
        printf("<p>文件未能上传成功</p>");
    }
}
?>
</body>
</html>

1