0

The file upload works with xampp in windows but not working when I moved it to a production centos server. It throws "invalid file" error. This is the code I am using:

<?php
  $allowedExts = array("gif", "jpeg", "jpg", "png");
  $temp = explode(".", $_FILES["file"]["name"]);
  $extension = end($temp);
  if ((($_FILES["file"]["type"] == "image/gif")
  || ($_FILES["file"]["type"] == "image/jpeg")
  || ($_FILES["file"]["type"] == "image/jpg")
  || ($_FILES["file"]["type"] == "image/pjpeg")
  || ($_FILES["file"]["type"] == "image/x-png")
  || ($_FILES["file"]["type"] == "image/png"))
  && ($_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("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
  }
  }
else
 {
 echo "Invalid file";
 }
 ?>

Can someone please help me with this........

10
  • 1
    check if upload folder is having write permission on production server. Commented Mar 19, 2014 at 6:29
  • and also check whether enctype="multipart/form-data" this set to Your form tag or not. Commented Mar 19, 2014 at 6:33
  • @AbhikChakraborty the folder has a 777 permission Commented Mar 19, 2014 at 6:34
  • @ripa Ya its there........ checked it Commented Mar 19, 2014 at 6:35
  • 1
    replace move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); with move_uploaded_file($_FILES["file"]["name"], "upload/" . $_FILES["file"]["name"]); --- try this and let me know Commented Mar 19, 2014 at 6:39

2 Answers 2

4

replace below line

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

with the below line

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

It'll solve Your problem.

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

2 Comments

@winnyboy5 this is the problem You are facing is permission issue. check projectpier.org/node/285
0

Locate the php.ini file and make sure that file_upload is enabled and not commented.

    sudo find / -name php.ini

edit:

sudo nano /etc/php/8.x or 7.4 your version/apache2/php.ini

uncomment:

file_upload=no
file_max_filesize=100M
max_file_uploads= 200

Make sure your php file upload script contains the correct upload folder directory (./upload/ or /upload or upload/).

Check if the upload folder has the required permission: "normally all files on a server must have the following permission: 644 and all folders 755"

sudo chmod 755 -R your-upload-folder

if notm try with chmod 777

    sudo chown username:usergroup your-upload-folder

restart your server.
Apache:

    sudo service apache2 restart

Nginx:

    sudo systemctl restart nginx

https://www.youtube.com/watch?v=hxsBJBa9bQM

Comments

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.