0

I am on Linux using apache, I copied and pasted the code from https://www.w3schools.com/php/php_file_upload.asp and It got the "Sorry, there was an error uploading your file error" that can be found at the last else statement.

here is the code:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

uploadform.php:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

I even tried changing $target_dir = "uploads/" to $target_dir = "/var/www/html/uploads/" because I created the uploads directory there. How can I get the the jpg file that I am trying to upload and save to the uploads folder on my local server.

RGBWaterBlock
  • 159
  • 2
  • 8

2 Answers2

1

This is your code:

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}

Since you get the second half of the if/else you know that move_uploaded_file has returned a false value.

So look at the documentation for that function:

Returns TRUE on success.

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

Since you've already done a bunch of checks on the file, you know that it is a valid upload file, so the third paragraph must be the one that explains the problem.

It says a warning will be issued, so turn on all errors and warnings and then look at the message that is output.

That will tell you what the problem is (I'd guess it is that the web server doesn't have write permission for the target directory).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I turned on errors and it gives the following errors: Warning: move_uploaded_file(uploads/lions.jpg): failed to open stream: Permission denied in /var/www/html/upload.php on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/phpYfPSqs' to 'uploads/lions.jpg' in /var/www/html/upload.php on line 40 Sorry, there was an error uploading your file. How can I give the folder permission? – RGBWaterBlock Sep 05 '17 at 23:44
  • https://stackoverflow.com/search?q=linux+web+server+permission+write+file – Quentin Sep 06 '17 at 08:24
1

Your output says that the file is uploaded to your temp directory - but the file could not be moved to the destination you have set.

Check the path where you want to move your file (e.g. $target_file) to and make sure that the web user (apache, www or whatever) has permissions to write to that directory.

lexotrion
  • 84
  • 1
  • 4