当我上传文件时,结果为空。 代码来自here。
index.html文件:
<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>
upload_file.php:
<?php
echo "Upload: " . $_FILES['file']['name'] . "<br>";
?>
结果是:
上载:
我配置我的php.ini,也启用file_upload,内存5000M和tmp位置。
我不知道它是maters但我使用的是Ubuntu。
phpinfo的结果就像我配置的那样。
怎么了?谢谢!
print_r是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
在原始文件中,错误日志为空。
答案 0 :(得分:1)
“当我上传文件时,结果为空。代码来自
here
。”
您说从W3Schools获取的代码不是您为代码发布的代码,而且您提到的代码仅为:
<?php
echo "Upload: " . $_FILES['file']['name'] . "<br>";
?>
W3Schools在其页面上(来自您提供的链接)的内容如下:
HTML表单:
<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"];
}
?>
然而,缺少最重要的组件move_uploaded_file()
包含在保存上传的文件下的页面下方,如下所示:(假设您只想在数组中允许("gif", "jpeg", "jpg", "png")
。
<?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";
}
?>
现在,您需要创建一个名为upload
的文件夹,然后从根目录运行表单和PHP,并确保该文件夹存在且具有正确的写入权限。
答案 1 :(得分:0)
你的代码很好。问题必须与服务器上的配置有关。在php.ini中检查这一行:
file_uploads,upload_max_filesize,upload_tmp_dir,post_max_size,max_input_time
你也可以回应这一行:
$_FILES['userfile']['error']