移动上传的文件无效

时间:2013-06-05 04:28:08

标签: php upload

我在这里查看了类似的问题,但似乎没有一个对我有用,我的代码不会将文件上传到新目录。它只返回设置错误“无法上传文件” 请帮忙

if ($_FILES["imgfile"]["type"] == "image/jpeg")
{
if ($_FILES["imgfile"]["error"] > 0)
{
echo "Error: " . $_FILES["imgfile"]["error"] . "<br />";
}
else
{
move_uploaded_file($_FILES["imgfile"]["tmp_name"],
"upload/" . $_FILES["imgfile"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["imgfile"]["name"];
echo "<br> File uploaded successfully.";
}
}
else
{
echo "File could not be uploaded.";
}

2 个答案:

答案 0 :(得分:1)

确保已将形式的multipart / form-data

答案 1 :(得分:0)

首先确保已将enctype属性设置为multipart / form-data, 试试这个,

$name=$_FILES['imgfile']['name'];
$path="upload/".$name;
$type=$_FILES['imgfile']['type'];
$pos=strpos($type,'image');
if($pos===false)
{
    //error
}
else
{
    move_uploaded_file($_FILES['imgfile']['tmp_name'],$path);
}