1

Always when I upload a file the result is empty. The code is from here.

The index.html file:

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

The upload_file.php:

<?php
  echo "Upload: " . $_FILES['file']['name'] . "<br>";
?>

The result is:

Upload:

I configure my php.ini, too enable file_upload, memory 5000M, and tmp location.

I don't know it maters but I use Ubuntu.

The result of phpinfo is as I configure.

What's wrong? Thanks!

print_r is show Array ()

echo $_FILES['userfile']['error'];

HP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626+lfs/msql.so' - /usr/lib/php5/20090626+lfs/msql.so: cannot open shared object file: No such file or directory in Unknown on line 0
    [Wed Mar 05 17:48:16 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.10 with Suhosin-Patch configured -- resuming normal operations
    [Wed Mar 05 17:50:05 2014] [error] [client 127.0.0.1] File does not exist: /var/www/favicon.ico
    [Wed Mar 05 17:50:05 2014] [error] [client 127.0.0.1] File does not exist: /var/www/404.html
    [Wed Mar 05 17:55:32 2014] [error] [client 127.0.0.1] PHP Notice:  Undefined index: file in /var/www/uploadart.php on line 2, referer: http://localhost/index.php

At my original file error log is empty.

11
  • 1
    Have you tried restarting Apache after changing your php.ini settings? Commented Mar 5, 2014 at 15:32
  • About 3 times. I tried that. Commented Mar 5, 2014 at 15:35
  • 1
    Show us the ouput of print_r($_FILES); like @OneOfOne suggested. Commented Mar 5, 2014 at 15:36
  • print_r is not showing enything just Array() Commented Mar 5, 2014 at 15:38
  • If that is your entire code, you are missing move_uploaded_file() <= do read the manual. @user3369031 - Amongst other things. That single line of code won't upload anything. If it isn't your full code, then do post it. Commented Mar 5, 2014 at 15:59

2 Answers 2

1

"Always when I upload a file the result is empty. The code is from here."

The code you say that you took from W3Schools is not what you posted for code and that you mention the code you are using is only:

<?php
  echo "Upload: " . $_FILES['file']['name'] . "<br>";
?>

What W3Schools has on their page (from the link you provided), is the following:

HTML form:

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

PHP

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

Yet this is missing the most important component move_uploaded_file()

which is included further down the page under Saving the Uploaded File and is as follows: (assuming you want to only allow ("gif", "jpeg", "jpg", "png") in the array.

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

Now, you need to create a folder called upload and then run the form and PHP from your root and make sure that the folder exists and has proper write permissions.

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

6 Comments

Invalid file all the time. move_uploaded_file() is change the file location not modify the output of $_FILES['file']['name'] witch is null. In the tmp folder is nothing.
Are you running the form and have the PHP in the root (and not in a sub-folder), and does the upload folder exist with proper write permissions? @user3369031 and did you copy everything exactly from my answer?
All yes. I triple checked everything befoure open this topic.
I remember seeing another question a week or so ago, about a similar problem where the OP was running it from Ubuntu server and had to change the folder permissions elsewhere and I can't remember what it was exactly. It's most likely the same thing happening. @user3369031
That ask was not on stackoverflow. I fix it and i started another project. It was not a bit asame i had a ftp that deny me to have 777 at /var/www/.
|
0

Your code is fine. The problem must be with configuration on your server. Check this lines in php.ini:

file_uploads, upload_max_filesize, upload_tmp_dir, post_max_size, max_input_time

You can also echo this line:

$_FILES['userfile']['error']

3 Comments

As in your list: on, 5000M, /var/www/test/tmp, 8M, 60. And the code is return nothing.
You just copied $_FILES['userfile']['error'] or used $_FILES['file']['error'] also, did you checked error logs?
Both, and both return null. On error logs have lots but i dont know what they mean. Posted up.

Your Answer

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