PHP move_uploaded_file() 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3501749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP move_uploaded_file() error?
提问by Awan
I using following code and it is successfully uploading files on my local machine. It is showing "Successfully uploaded" on my local machine.
我使用以下代码,它成功地在我的本地机器上上传文件。它在我的本地机器上显示“已成功上传”。
// Upload file
$moved = move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . "myFile.txt" );
if( $moved ) {
echo "Successfully uploaded";
} else {
echo "Not uploaded";
}
But when I used this code on my online server then it is not uploading file and just showing message "Not uploaded".
但是当我在我的在线服务器上使用此代码时,它不会上传文件而只显示消息“未上传”。
How can I know that what is the problem and how can I get the actual problem to display to the user ?
我如何知道问题出在哪里以及如何将实际问题显示给用户?
回答by Denver Chiwakira
Edit the code to be as follows:
编辑代码如下:
// Upload file
$moved = move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . "myFile.txt" );
if( $moved ) {
echo "Successfully uploaded";
} else {
echo "Not uploaded because of error #".$_FILES["file"]["error"];
}
It will give you one of the following error code values 1 to 8:
它将为您提供以下错误代码值 1 到 8 之一:
UPLOAD_ERR_INI_SIZE = Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
UPLOAD_ERR_FORM_SIZE = Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
UPLOAD_ERR_PARTIAL = Value: 3; The uploaded file was only partially uploaded.
UPLOAD_ERR_NO_FILE = Value: 4; No file was uploaded.
UPLOAD_ERR_NO_TMP_DIR = Value: 6; Missing a temporary folder. Introduced in PHP 5.0.3.
UPLOAD_ERR_CANT_WRITE = Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
UPLOAD_ERR_EXTENSION = Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help.
UPLOAD_ERR_INI_SIZE = 值:1;上传的文件超过了 php.ini 中的 upload_max_filesize 指令。
UPLOAD_ERR_FORM_SIZE = 值:2;上传的文件超过了在 HTML 表单中指定的 MAX_FILE_SIZE 指令。
UPLOAD_ERR_PARTIAL = 值:3;上传的文件只是部分上传。
UPLOAD_ERR_NO_FILE = 值:4;没有上传文件。
UPLOAD_ERR_NO_TMP_DIR = 值:6;缺少临时文件夹。在 PHP 5.0.3 中引入。
UPLOAD_ERR_CANT_WRITE = 值:7;无法将文件写入磁盘。在 PHP 5.1.0 中引入。
UPLOAD_ERR_EXTENSION = 值:8;PHP 扩展停止了文件上传。PHP 没有提供确定哪个扩展名导致文件上传停止的方法;使用 phpinfo() 检查加载的扩展列表可能会有所帮助。
回答by Andrei Serdeliuc ?
Check that the web server has permissions to write to the "images/" directory
检查 Web 服务器是否具有写入“images/”目录的权限
回答by Martin Bean
Try this:
尝试这个:
$upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/images/";
if (is_dir($upload_dir) && is_writable($upload_dir)) {
// do upload logic here
} else {
echo 'Upload directory is not writable, or does not exist.';
}
This will instantly flag any file permission errors.
这将立即标记任何文件权限错误。
回答by Your Common Sense
How can I know that what is the problem
我怎么知道是什么问题
Easy. Refer to the error log of the webserver.
简单。请参阅网络服务器的错误日志。
how can I get the actual problem to display to the user ?
我怎样才能让实际问题显示给用户?
NEVERdo it.
An average user will unerstand nothing of this error.
A malicious user should get no feedback, especially in a form of very informative error message.
永远不要这样做。
普通用户不会理解这个错误。
恶意用户不应该得到任何反馈,尤其是以非常有用的错误消息的形式。
Just show a page with excuses.
只需显示一个带有借口的页面。
If you don't have access to the server's error log, your task become more complicated.
There are several ways to get in touch with error messages.
如果您无权访问服务器的错误日志,您的任务就会变得更加复杂。
有多种方法可以与错误消息取得联系。
To display error messages on screen you can add these lines to the code
要在屏幕上显示错误消息,您可以将这些行添加到代码中
ini_set('display_errors',1);
error_reporting(E_ALL);
or to make custom error logfile
或制作自定义错误日志文件
ini_set('log_errors',1);
ini_set('error_log','/absolute/path/tp/log_file');
and there are some other ways.
but you must understand that without actual error message you can't move. It's hard to be blind in the dark
还有其他一些方法。
但您必须明白,如果没有实际的错误消息,您将无法移动。很难在黑暗中失明
回答by Ali Saeed
move_uploaded_file()
will return:
move_uploaded_file()
将返回:
FALSE
if file name is invalidFALSE
andissue a warning
in the error log if the apache process does not have read/write permissions to source or destination directories
FALSE
如果文件名无效FALSE
并issue a warning
在错误日志如果Apache进程没有读/写权限的源或目标目录
PHP Error Log
PHP错误日志
My php error log was at: /var/log/httpd/error_log
and had these errors:
我的 php 错误日志在:/var/log/httpd/error_log
并且有这些错误:
Warning: move_uploaded_file(images/robot.jpg): failed to open stream: Permission denied in /var/www/html/mysite/mohealth.php on line 78
Warning: move_uploaded_file(): Unable to move '/tmp/phpsKD2Qm' to 'images/robot.jpg' in /var/www/html/mysite/mohealth.php on line 78
警告:move_uploaded_file(images/robot.jpg):无法打开流:第 78 行的 /var/www/html/mysite/mohealth.php 中的权限被拒绝
警告:move_uploaded_file():无法将第 78 行 /var/www/html/mysite/mohealth.php 中的“/tmp/phpsKD2Qm”移动到“images/robot.jpg”
move_uploaded_file()
tries to move files from a temporary directory to a destination directory. When apache process tried to move files, it could not read the temporary or write to the destination dir.
move_uploaded_file()
尝试将文件从临时目录移动到目标目录。当 apache 进程试图移动文件时,它无法读取临时文件或写入目标目录。
Find which user is running Apache (Web Server)Check which user is running the apache service by this command:
ps aux | grep httpd
. The first column is the user name.Check Read Permission at Temporary Dir:Your can find the path to your temp dir by calling
echo sys_get_tmp_dir();
in a php page. Then on the command line, issuels -ld /tmp/temporary-dir
to see if the apache user has access to read hereCheck Write Permission at Destination Dir:issue
ls -ld /var/www/html/destination-directory
to see if the apache user has access to write hereAdd permissions as necessary using
chown
orchgrp
Restart Apacheusing
sudo service httpd restart
查找哪个用户正在运行 Apache(Web 服务器)通过以下命令检查哪个用户正在运行 apache 服务:
ps aux | grep httpd
。第一列是用户名。检查临时目录的读取权限:您可以通过调用
echo sys_get_tmp_dir();
php 页面找到临时目录的路径。然后在命令行上,发出ls -ld /tmp/temporary-dir
来看看 apache 用户是否有权阅读这里检查写权限在目标方向:问题
ls -ld /var/www/html/destination-directory
,看看apache用户可以访问这里写根据需要使用
chown
或添加权限chgrp
使用重新启动Apache
sudo service httpd restart
回答by Piotr Müller
Do you checks that file is uploaded ok ? Maybe you exceeded max_post_size, or max_upload_filesize. When login using FileZilla you are copying files as you, when uploading by PHP wiritng this file is from user that runs apache (for exaplme www-data), try to put chmod 755 for images.
你检查文件上传好了吗?也许您超出了 max_post_size 或 max_upload_filesize。使用 FileZilla 登录时,您正在复制文件,当通过 PHP wiritng 上传此文件时,该文件来自运行 apache 的用户(例如 www-data),请尝试将 chmod 755 用于图像。
回答by Jeffery Dilegge
or run suexec and never have to change permissions again.
或者运行 suexec 并且永远不必再次更改权限。
回答by dhitro
Please check that your form
tag have this attribute:
请检查您的form
标签是否具有此属性:
enctype="multipart/form-data"
enctype="multipart/form-data"
回答by Christopher John Gamo
$uploadfile = $_SERVER['DOCUMENT_ROOT'].'/Thesis/images/';
$profic = uniqid(rand()).$_FILES["pic"]["name"];
if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
{
$moved = move_uploaded_file($_FILES["pic"]["tmp_name"], $uploadfile.$profic);
if($moved)
{
echo "sucess";
}
else
{
echo 'failed';
}
}
回答by Minherc
On virtual hosting check your disk quota
.
在虚拟主机上检查您的disk quota
.
if quota exceed, move_uploaded_file
return error.
如果配额超过,则move_uploaded_file
返回错误。
PS : I've been looking for this for a long time :)
PS:找了很久了:)