php move_uploaded_file 不起作用,没有错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5655859/
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
move_uploaded_file doesn't work, no error
提问by David
I am running running a script which moves an uploaded file with move_uploaded_file()
. I have done this thousands of times but for some reason it's not working. I have confimred the following:
我正在运行一个脚本,该脚本使用move_uploaded_file()
. 我已经这样做了数千次,但由于某种原因它不起作用。我已确认以下内容:
<form>
usingmethod="post"
and correctenctype
- correct file referenced from form
- directory has permissions
777
- all the
memory_limit
,max_execution_time
, etc are set to super high settings to avoid timeouts
<form>
使用method="post"
和纠正enctype
- 从表单引用的正确文件
- 目录有权限
777
- 所有
memory_limit
,max_execution_time
, 等都设置为超高设置以避免超时
Basically, the script below returns with just Your image is too big.
. I have also enabled ALL errors to display and still don't get an error. Any ideas?
基本上,下面的脚本只返回Your image is too big.
. 我还启用了显示所有错误,但仍然没有出现错误。有任何想法吗?
$time = time();
$target_path = "/absolute/path/to/temp/directory/temp/";
$target_path = $target_path.$time.'.jpg';
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
} else{
$error .= '<li>Your image is too big.</li>';
}
Using 1and1 hosting with the php.ini hack :P
使用 1and1 托管与 php.ini hack :P
UPDATE 1
更新 1
I would like to add that response from the script occurs exactly after 60 seconds.
我想添加脚本的响应恰好在 60 秒后发生。
UPDATE 2
更新 2
We might be getting somewhere with this. Just print_r($_FILES)
and this is the result of the array:
我们可能会有所收获。只是print_r($_FILES)
,这是数组的结果:
Array (
[image] => Array (
[name] => P2120267.JPG
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
So that leads me to believe that the file isn't be uploaded correctly to the server or something? I have checked and the post form is <form action="" method="post" enctype="multipart/form-data">
. So, from what I can tell, the file isn't being uploaded to the server's temp area?
所以这让我相信文件没有正确上传到服务器或其他什么?我已经检查过了,邮寄表格是<form action="" method="post" enctype="multipart/form-data">
. 所以,据我所知,文件没有上传到服务器的临时区域?
UPDATE 3
更新 3
Noticed the [error] => 1
in the above array. This is apparently down to the filesize being larger than the upload_max_filesize
. However, when i set this as 128M
, I get a white screen of death after 60 seconds. The file I'm uploading is 2.5MB
注意到[error] => 1
上面数组中的 。这显然是由于文件大小大于upload_max_filesize
. 但是,当我将其设置为 时128M
,60 秒后出现白屏死机。我上传的文件是 2.5MB
Here is my php.ini file:
这是我的 php.ini 文件:
register_globals=off
memory_limit = 128M
max_execution_time=3600
post_max_size = 128M
upload_max_filesize= 128M
UPDATE 4
更新 4
With details above, it appears that I am getting a WSOD, but the image is being upoaded. So, how to stop the WSOD? I can't find any errors related anywhere.
根据上面的详细信息,我似乎收到了 WSOD,但正在上传图像。那么,如何阻止WSOD?我在任何地方都找不到任何相关的错误。
UPDATE 5 - FOUND IT!
更新 5 - 找到了!
Shame on me for not giving you guys all the code. It looks like its to do with this line:
没有给你们所有的代码让我感到羞耻。它看起来与这一行有关:
resizeImage($feedBurnerStatsSource, PHOTOMSGDIR.'temp/'.$time.'-tmp.jpg',$width,$height);
In the following code:
在以下代码中:
function resizeImage($source, $destination = NULL,$wdt, $height = NULL){
if(empty($height)){
// Height is nit set so we are keeping the same aspect ratio.
list($width, $height) = getimagesize($source);
if($width > $height){
$w = $wdt;
$h = ($height / $width) * $w;
$w = $w;
}else{
$w = $wdt;
$h = $w;
$w = ($width / $height) * $w;
}
}else{
// Both width and Height are set.
// this will reshape to the new sizes.
$w = $wdt;
$h = $height;
}
$source_image = @file_get_contents($source) or die('Could not open'.$source);
$source_image = @imagecreatefromstring($source_image) or die($source.' is not a valid image');
$sw = imagesx($source_image);
$sh = imagesy($source_image);
$ar = $sw/$sh;
$tar = $w/$h;
if($ar >= $tar){
$x1 = round(($sw - ($sw * ($tar/$ar)))/2);
$x2 = round($sw * ($tar/$ar));
$y1 = 0;
$y2 = $sh;
}else{
$x1 = 0;
$y1 = 0;
$x2 = $sw;
$y2 = round($sw/$tar);
}
$slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimmensions');
imagecopyresampled($slate, $source_image, 0, 0, $x1, $y1, $w, $h, $x2, $y2);
// If $destination is not set this will output the raw image to the browser and not save the file
if(!$destination) header('Content-type: image/jpeg');
@imagejpeg($slate, $destination, 75) or die('Directory permission problem');
ImageDestroy($slate);
ImageDestroy($source_image);
if(!$destination) exit;
return true;
}
So, WSOD means that its some sort of die without a message. Any ideas?
所以,WSOD 意味着它在没有消息的情况下死亡。有任何想法吗?
回答by sbditto85
Just to verify is the post_max_filesize
set to a high level? Because according to php.net:
只是为了验证post_max_filesize
设置是否为高电平?因为根据php.net:
If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty.This can be tracked in various ways, e.g. by passing the
$_GET
variable to the script processing the data, i.e.<form action="edit.php?processed=1">
, and then checking if$_GET['processed']
is set.
如果 post 数据的大小大于 post_max_size,则 $_POST 和 $_FILES 超全局变量为空。这可以通过多种方式来跟踪,例如通过将
$_GET
变量传递给处理数据的脚本,即<form action="edit.php?processed=1">
,然后检查是否$_GET['processed']
设置。
Something to consider.
需要考虑的事情。
for more info see this linkand scroll down to the post_max_filesize
section
有关更多信息,请参阅此链接并向下滚动到该post_max_filesize
部分
UPDATE
更新
In my experience if you're getting a WSOD it's usually do to error_reporting
and display_errors
being turned off OR the memory_limit
being reached. In the script at the top I usually set the memory_limit
to 1024M to verify that isn't the problem and the turn on error_reporting
and display_errors
... so put this before the file upload:
根据我的经验,如果你得到一个WSOD它通常做的error_reporting
和display_errors
被关闭或者memory_limit
被达到。在顶部的脚本,我通常设置memory_limit
到1024M,以验证是没有问题的上翻error_reporting
和display_errors
...文件上传之前,所以把这个:
error_reporting(E_ALL); // or E_STRICT
ini_set("display_errors",1);
ini_set("memory_limit","1024M");
That normally gets rid of the WSOD and gives you and error to work with.
这通常会摆脱 WSOD 并为您提供和错误处理。
UPDATE
更新
Have you tried taking off the @
error suppression in front of all your functions to see they are producing a specific error? Also what are you execution and input timeouts? and Can you verify what headers are being sent? (make sure it is Content-Type=text/html;
)
您是否尝试@
在所有函数前取消错误抑制以查看它们是否产生特定错误?另外你的执行和输入超时是什么?你能验证发送的是什么标头吗?(确保它是Content-Type=text/html;
)
回答by Brant Messenger
Try changing the target path to a non temp directory.
尝试将目标路径更改为非临时目录。
After some of the updates here is the resolution:
经过一些更新,这里是解决方案:
set_time_limit(0);
ini_set('upload_max_filesize', '500M');
ini_set('post_max_size', '500M');
ini_set('max_input_time', 4000); // Play with the values
ini_set('max_execution_time', 4000); // Play with the values
...add this to the beginning of your file that processes the upload.
...将其添加到处理上传的文件的开头。
回答by Your Common Sense
[error] => 1
means The uploaded file exceeds the upload_max_filesize directive in php.ini.
http://www.php.net/manual/en/features.file-upload.errors.php
[error] => 1
表示上传的文件超过了 php.ini 中的 upload_max_filesize 指令。
http://www.php.net/manual/en/features.file-upload.errors.php
So, you have to change your settings.
因此,您必须更改设置。
as for the php.ini file you posted here, it just doesn't affect your PHP. you have to move it somewhere to more proper location
至于您在此处发布的 php.ini 文件,它不会影响您的 PHP。你必须把它移到更合适的位置
回答by Daniel E.
I had the same problem last week, it was just because my server space disk was full! I hope it will help someone...
我上周也遇到了同样的问题,只是因为我的服务器空间磁盘已满!我希望它会帮助某人...
回答by blu
I had same problem, but I wanted to overwrite a file, so I have to delete old file and than it works.
我有同样的问题,但我想覆盖一个文件,所以我必须删除旧文件,然后才能正常工作。