Reputation: 1625
I've got the following code to upload a file and add it to the folder called images
located in the root of the server.
$file = $_FILES['prodImg']['tmp_name'];
$newLoc="/images/" . $_FILES['prodImg']['name'];
if(move_uploaded_file($file, $newLoc)){
//do some other code here
}
else{
echo 'error';
}
the form has this button to add the image
<input type="file" name="prodImg" id="prodImg" accept="image/png" />
the images
folder has all permissions set to read, write and execute
every time i try to upload an image it go to the else
statement.
not sure what i'm doing wrong here.
how do i make it work properly?
Upvotes: 1
Views: 1660
Reputation: 2813
remove the slash ahead of images
$newLoc= "images/".$_FILES['prodImg']['name'];
Upvotes: 0
Reputation: 3783
The path of your $newLoc
variable may be wrong. Try ./images/
.
The one specified may define an image
folder on the same level as the home
, root
... folders if you use Linux.
Upvotes: 0
Reputation: 7587
Do you have attribute in your form?
enctype="multipart/form-data"
I.e.:
<form action="..." method="post" enctype="multipart/form-data">
Then you can try to check permission for that folder.
And also try change filepath:
$newLoc="./images/" . $_FILES['prodImg']['name'];
or
$newLoc="images/" . $_FILES['prodImg']['name'];
Upvotes: 1