Reputation: 121
I have a problem with moving uploaded files.
<?php
$image_name = $_FILES['image']['name'] ;
$target_file = "../uploads/$image_name";
$targetFileForItem = "uploads/$image_name";
move_uploaded_file($_FILES['image']['tmp_name'], $target_file);
$sql = "INSERT INTO items (name , description,`price`, `country`, `release`, `condition`, `image`)
VALUES ('$name','$description','$price', '$country', '$date', '$condition', '$targetFileForItem')" ;
?>
the variable $targetFileForItem
works currect, and inserts into my db very well, but the file don't move into $target_file
var's folder, which is uploads
. As you see I use move_uploaded_file()
function, but i's not working. Any suggestions?
Upvotes: 1
Views: 7523
Reputation: 2158
Check your permission for upload folder it must be 775. If you are using FTP than right click on folder and change File permission of that folder to 755. If it's localhost then it must be a path or folder name issue.
And make your code like this so you can get find out errors also.
<?php
$image_name = $_FILES['image']['name'] ;
$target_file = "../uploads/$image_name";
$targetFileForItem = "uploads/$image_name";
// if folder not exists than it will make folder.
if(!file_exists($target_file))
{
mkdir($target_file, 0777, true);
}
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_file))
{
echo "file successfully uploaded";
}
else
{
echo "error in file upload";
}
?>
Upvotes: 1
Reputation: 7294
Write this to debug
ini_set('display_errors',1);
error_reporting(E_ALL);
If your code is ok then check file permissions
you can use this
if (is_dir($target_file ) && is_writable($target_file )) {
// do upload logic here
} else {
echo 'Upload directory is not writable, or does not exist.';
}
is_writable
Returns TRUE
if the filename exists and is writable
. The filename argument may be a directory name allowing you to check if a directory is writable
for more info read this http://php.net/manual/en/function.is-writable.php
Upvotes: 1