Mauro F. Mauro F. - 4 months ago 9
PHP Question

PHP move_uploaded_file() deosn't work

I'm trying to upload file to my Raspberry Pi 3. I installed Apache and PHP and this is the content of the page index.php:

<form enctype="multipart/form-data" action="index.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

<?php
if ($_FILES["userfile"]["size"] != 0 && $_FILES["userfile"]["tmp_name"] != "")
{
$uploaddir = "/var/www/html/";
$uploadfile = $uploaddir . $_FILES["userfile"]["name"];
try
{
echo '<pre>';
if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploadfile))
{
echo "File is valid, and was successfully uploaded.\n\n";
}
else
{
echo "File upload failed\n\n";
}
}
catch (Exception $e) { echo $e->getMessage()."\n"; }

echo "Array content:\n";
print_r($_FILES);

print "</pre>";
} ?>


When I upload a file, even a text file with some bytes in it, move_uploaded_file() returns false and the file is not uploaded, however no exception is thrown and the value of
$_FILES["userfile"]["error"]
is 0. The permission of the folder
/var/www/html/
are
drwxr-xr-x
, so they should be enough, right?

Answer

Apparently it was enough to run sudo chmod 777 /var/www/html/. I was quite sure that I did that already but probably I didn't do it correctly.

Comments