Reputation: 17
I've looked around similar questions to this one here, but none seems to work for me, my code does not upload file to new directory. It only returns the set error "file could not be uploaded" Please help
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.";
}
Upvotes: 0
Views: 267
Reputation: 1400
First make sure you have set the enctype attribute to multipart/form-data, then try this,
$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);
}
Upvotes: 0
Reputation: 353
Make sure you have set the enctype attribute to multipart/form-data in the form
Upvotes: 1