I've been struggling with this for more than 8h now, so I hope someone can help me. I think I went through all the posts regarding this issue, but wasn't able to find a solution.
There are no errors, both /tmp
and /var/www/test
are owned by apache (www-data
) and have 775 permission set.
This is the PHP code:
$target_dir = $_SERVER["DOCUMENT_ROOT"] . "test/uploads/";
$target_file = $target_dir . basename($_FILES["pic"]["name"]);
$uploadOK = 1;
$image_file_type = pathinfo($target_file, PATHINFO_EXTENSION);
if(isset($_POST['submit'])) {
if($uploadOK === 0) {
echo "Upload failed!";
} else {
// This is where it stops...
// There is definitely something wrong with $target_file
if(move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file)) {
echo "Success";
} else {
echo "Not uploaded...<br>";
echo move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file) . "<br>";
echo $target_file . "<br>";
print_r($_FILES) . "<br>";
error_reporting(E_ALL);
}
}
These are the results I get:
Not uploaded...
/var/www/test/uploads/test.jpg
Array (
[pic] => Array (
[name] => test.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpKlzCxc
[error] => 0
[size] => 721090
)
)
So, what am I doing wrong?
Ok, here are the updates:
- The checking of the upload was done at the beginning of the script and it worked which is why I didn't include it in the question.
- This is the code from www.w3schools.com, and I will be checking out the other source (thanks for pointing that out)
- After fixing the position of the
error_reporting
(good call :)), finally got a whole bunch of "Permission denied" warnings as I suspected would be the problem, and here they are:
Warning: move_uploaded_file(/var/www/test/uploads/test.jpg): failed to open stream: Permission denied in /var/www/test/upload.php on line 47
Warning: move_uploaded_file(): Unable to move '/tmp/phpHzvhOA' to '/var/www/test/uploads/test.jpg' in /var/www/test/upload.php on line 47
Line 47:
if(move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file)) {
Permission set for /var/www/test/upload.php:
-rwxrwxr-x 1 www-data www-data 2174 Sep 9 00:07 /var/www/test/upload.php