Search code examples
phpapache-commons-fileupload

Php Upload in Linux not working


I have this PHP upload script. I can find the JPG but it will not upload the picture. And I am trying to do this on localhost on a Linux mint machine, so my code is at /var/www/html/uploads. Here is my code for upload.php.

<?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.";
  }
}
?> 

And here is my code for media_upload.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> 

Here is the error messages from my apache2 error log. I think my problem maybe permissions or a path problem. All help greatly appreciated.

PHP Warning: move_uploaded_file(uploads/bedshaper_1021-36.jpg): failed to open stream: No such file or directory in /var/www/html/uploads/upload.php on line 38, referer: http://localhost/uploads/media_upload.php

PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpXeCU8c' to 'uploads/bedshaper_1021-36.jpg' in /var/www/html/uploads/upload.php on line 38, referer: http://localhost/uploads/media_upload.php


Solution

  • You must create the folder "uploads" in "/var/www/html/uploads" then it should works. You also can change

    $target_dir = "uploads/";
    

    to

    $target_dir = "";
    

    If you want to save the picture in "/var/www/html/uploads" you should make a direct path.

    $target_dir = "var/www/html/uploads";
    

    EDIT Since your upload script is inside the upload folder you're referring to, the script doesn't find the folder.

    $target_dir = "../uploads"
    

    will also work