1

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.

4
  • Can you indent your codes properly? Commented Mar 25, 2014 at 8:45
  • Multiple PHP issues here: 1. You didn't check the result of move_uploaded_file(); 2. you chmod file to 777 which poses security error; 3. (recommendation) you didn't call exit after header('Location: ...') Commented Mar 25, 2014 at 8:54
  • @Raptor done. the uploaded file is a json file with 1.02kb size Commented Mar 25, 2014 at 8:54
  • @Raptor I used 777 to check weather the issue is caused by permissions Commented Mar 25, 2014 at 8:56

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Tried it already. it executed the code but haven't uploaded the file
Check for your folder permissions, or make a look at this projectpier.org/node/285
2

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.

2 Comments

Checked it and found the source.the file type shows as "application/octet-stream" instead of "application/json"
application/octet-stream is a binary byte file. Are you sure your uploaded file is a ASCII-encoded text file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.