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