我正在尝试测试一个简单的文件上传脚本,但它失败了。这是html部分。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title="testing"></title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input type="file" name="uploaded"/>
<input type="submit" value="Upload File"/>
</form>
</body>
下面是php部分。
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
在这一行失败了:
if(move_uploaded_file($ _ FILES ['uploaded'] ['tmp_name'],$ target))
执行else部分:“抱歉,上传文件时出现问题。”如何让它无故障地工作。谢谢你的帮助。
答案 0 :(得分:0)
最可能的原因是缺少对目标目录的写访问权。
另外,我会检查$target
是否正确解析,因为文件名中可能存在似乎直接传递给脚本并且未被过滤的工件。
答案 1 :(得分:0)
您可以使用以下方式测试权限 is_writable(PATH_OF_FOLDER)
我也倾向于测试文件的存在性 is_file(PATH_OF_NEW_FILE)
确认上传成功之前!