1

I followed all the steps from tutorial WikiHow - Guide to install PHP

I can execute normal php commands, but can't upload files using $_FILE command.It always gives me error.

My File uploading script is

<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="file_uploader.php" method="post"
                        enctype="multipart/form-data">
<input type="file" name="file"/>
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

and my file_uploader.php file code is

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if(1)
  {
    print_r($_FILES);

  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];

    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";
  }
?>

In above code , you can see i set condition to always true,so it doesn't give any error but also it doesn't upload file.

the output of it is

Array ( [file] => Array ( [name] => college.jpg [type] => image/jpeg [tmp_name] => /tmp/php4aaDd6 [error] => 0 [size] => 113731 ) ) /tmpUpload: college.jpg
Type: image/jpeg
Size: 111.0654296875 kB
Stored in: /tmp/php4aaDd6 Stored in: upload/college.jpg

and inspire of this output , I can't find file at that location.

My php.ini file configuration can be seen php.ini - Pastebin URL

8
  • 1
    try changing the first if-statement to just if ( true ). Alternatively just do a print_r($_FILES); to see if any file has been uploaded.
    – Gerben
    Commented Mar 1, 2014 at 20:01
  • the array gives following description , [name] => Icon.png [type] => image/png [tmp_name] => /tmp/phpR2tspv [error] => 0 [size] => 393576 ) , that means file is received but still I can't find it in upload folder.... @Gerben
    – HarshIT
    Commented Mar 1, 2014 at 20:21
  • The example code you are using only accepts 20kb files. Icon.png is 394kb. That is why it is not working! Just change the 20000 to some larger number.
    – Gerben
    Commented Mar 2, 2014 at 9:45
  • @Gerben , I have edited my question , it contains my uploading logic.
    – HarshIT
    Commented Mar 2, 2014 at 17:14
  • Have you changed the permissions of the upload folder? Webserver run under a different username, so by default, they can't write to a folder you (as user pi) have created. Just run chmod a+x upload
    – Gerben
    Commented Mar 2, 2014 at 18:18

0

Your Answer

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

Browse other questions tagged or ask your own question.