Reputation: 73
I want to make a website where I can upload files to a specific directory on my Linux server.
HTML:
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<p>File: </p>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000000000000">
<input type="file" name="FileToUpload" id="FileToUpload"> <br> <br>
<input type="submit" value="Hochladen" name="submit">
</form>
</body>
PHP:
<?php
$directory = "var/www/html/upload/";
$file = $directory . basename($_FILES["FileToUpload"]["name"]);
if(move_uploaded_file($_FILES["FileToUpload"]["name"], $file)){
echo "File was succesfully uploaded!";
}
else{
echo "ERROR";
}
print_r(error_get_last());
print_r($_FILES);
?>
Php.ini:
file_uploads = On
upload_tmp_dir= "/var/www/html/upload/files/"
upload_max_filesize = 2048M
max_file_uploads = 20
output:
Array ( [FileToUpload] => Array ( [name] => Meme.jpg [type] => image/jpeg [tmp_name] => /var/www/html/upload/files/phpiQizaE [error] => 0 [size] => 91487 ) )
The folder for the uploaded image has 777
permissions and www-data
user and group.
I don't know how to make it work.
I think it's something I must change in linux, because in the middle of the search it says The upload was succesfull
, so I don't think the php or the html are the problem...
Really appreciate any kind of help.
Upvotes: 0
Views: 1435
Reputation: 897
I tried your code and found 2 flaws:
The parameter 1 should have tmp_name instead of name like this: move_uploaded_file($_FILES["FileToUpload"]["tmp_name"], $file)
in place of move_uploaded_file($_FILES["FileToUpload"]["name"], $file)
The location should be absolute so it should be $directory = "/var/www/html/upload/";
instead of $directory = "var/www/html/upload/";
The code will work. At least it worked in mine.
Upvotes: 2
Reputation: 984
You should not move file by name, but by tmp_name, rewrite this part of code to look like this:
if(move_uploaded_file($_FILES["FileToUpload"]["tmp_name"], $file)){
echo "File was succesfully uploaded!";
}
Upvotes: 0