简体   繁体   中英

PHP file upload not working in centos server

These are the codes i use for a file upload. Everything works fine in Xammp windows. But its not working under centos server. It throws the error "Invalid file".

upload.php

   <?php

    $allowedExts = array("json");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);

    if ((($_FILES["file"]["type"] == "application/json"))
          && ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts))
    {

      if ($_FILES["file"]["error"] > 0)
       {
         echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
       }

       else
         {
           echo "Upload: " . $_FILES["file"]["name"] . "<br>";
           echo "Type: " . $_FILES["file"]["type"] . "<br>";
           echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
           echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

          if (file_exists("uploads/" . $_FILES["file"]["name"]))
            {
             echo $_FILES["file"]["name"] . " already exists. ";
            }
            else
            {
           $dir="uploads";
               if ($handle = opendir($dir)) {
               while (false !== ($entry = readdir($handle))) {
               if ($entry != "." && $entry != "..") {
           unlink("uploads/$entry");
            }
          }
          closedir($handle);
        }             
         move_uploaded_file($_FILES["file"]["tmp_name"],
         $f="uploads/" . $_FILES["file"]["name"]);
     chmod($f, 0777);
         echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
     header("Location: user.php");
        }
      }
   }
  else
   {
     echo "Invalid file";
   }


  ?>

HTML

     <form action="upload.php" method="POST" enctype="multipart/form-data">
     <label for="file"><span style="color:#ffffff;">Upload File:</span></label>
     <input type="file" name="file" id="file">
     <input class="btn btn-success btn-block" type="submit" name="submit" value="Submit">

The uploaded file is a Json file and its file size is 1.02kb.

Someone please help me solve this.

Use this it will work,

Replace below line,

move_uploaded_file($_FILES["file"]["tmp_name"], $f="uploads/" . $_FILES["file"]["name"]);

with the below line,

move_uploaded_file($_FILES["file"]["name"], "uploads/" . $_FILES["file"]["name"]);

And as well as check for your folder permissions, or any further doubts make a look at this,

http://www.projectpier.org/node/285

If the program flow goes into "Invalid File" case, it means either:

Assume that you choose a valid file to upload

  1. $_FILES["file"]["type"] is not application/json
  2. $_FILES["file"]["size"] >= 20000 bytes
  3. $extension is not json

To debug,

  1. echo $_FILES["file"]["type"]
  2. echo $_FILES["file"]["size"]
  3. echo $extension

Currently there is not enough details for further diagnose your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM