1

In PHP, I'm trying to save a file to the /var/www/upload directory, but whatever I try, it doesn't work.

I've tried giving www-data ownership of /var/www and subdirectories. I've tried modifying the code, to no avail. (Running Ubuntu Server 13.10)

Here's my code: (I'm trying to upload a png file.)

<?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
      {
      $hey = move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      echo "\n" . $hey;
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Any help appreciated.

user3079344
  • 401
  • 1
  • 4
  • 7
  • Welcome to Ask Ubuntu. Can you please tell us what error message you get? "I doesn't work" isn't enough to work with. What do you do and what happens then? What result do you expect? Please [edit your](http://askubuntu.com/posts/390842/edit) question and add the information. – MadMike Dec 16 '13 at 16:17
  • 1
    possible duplicate of [Whats the simplest way to edit and add files to "/var/www"?](http://askubuntu.com/questions/19898/whats-the-simplest-way-to-edit-and-add-files-to-var-www) – Parto Aug 23 '14 at 08:01

0 Answers0