【发布时间】:2014-05-29 14:06:01
【问题描述】:
当我在浏览器上使用 wampserver 打开文件时,它会显示: “解析错误:语法错误,第 53 行 C:\wamp\www\Tests\FileUploadPractiseForm.php 中的文件意外结束。”
我在文件正文中使用 php 脚本。
不知道是什么问题。 代码如下:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
<?php
if (isset($_POST["submit"]))
{
$name=$_FILES["file"]["name"];
$tmp_name=$_FILES["file"]["tmp_name"];
$error=$_FILES["file"]["error"];
$size=$_FILES["file"]["size"];
$type=$_FILES["file"]["type"];
$allowedExts = array("gif", "jpeg", "jpg", "png"); //Specify allowed Extensions
$temp = explode(".", $name); //Split up file name
$extension = end($temp); //Assign to last element of array with file-name parts ie extension.
if ( (($type == "image/gif")||($type == "image/jpeg")||($type == "image/jpeg")||($type == "image/png")||($type == "image/x-png")||($type == "image/pjepg")) && ($size < 1048476) && in_array($extension, $allowedExts) ) //check if its an image, if its less than max size, if its extension is allowed
{
if ( $error > 0)
{
echo "Error: " . $error . "<br>"; //in case there's an upload error
}
else if (isset($name))
{
if (!empty($name))
{
$location='upload/';
move_uploaded_file($tmp_name, $location.$name);
echo "Image Uploaded"; //this if-else section is used to store the file in non-temp location ie "upload" folder
}
}
}
else
{
if ($size >= 1048476)
{
echo "Maximum File Size Exceeded";
}
else
{
echo "Invalid File Type";
}
}
?>
</form>
</body>
</html>
【问题讨论】:
-
您的
{和}字符不平衡。 -
如果
if (isset($_POST["submit"])),您忘记关闭它
标签: php html file-upload eof