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........

winnyboy5
  • 1,486
  • 3
  • 22
  • 45

2 Answers2

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.

Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
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

Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88