I am trying to upload image as shown here w3schools But it always shows the error
Sorry, there was an error uploading your file.
PHP
<?php
if(!isset($_POST["submit"])){
die('Error');
}
$target_dir = "/var/www/img/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
It had Worked in Windows 10. Now the error seems upload is done but moving file or something else of the error..
OS : Ubuntu 18.04
PHP ver : 7.3
Apache2
In php.ini, file_uploads = On is setted.
2
Answers
The documentation page for the function you are using
move_uploaded_file()
describes the behavior in case of the failure.The first thing you could do is to enable error and warning reporting so you could see whether a warning is raised.
That way you will know whether an issue is with the filename or with actual file movement – which would indicate permissions issue.
It may be due to permission to directory you created in /var/www/ folder.
You can check your apache user by using following command
You will get the apache user.After that give permission to apache user to access the directory.In my case apache user is daemon so i give permission to this user to access the img directory.