【问题标题】:move_uploaded_file doesn't work, no errormove_uploaded_file 不起作用,没有错误
【发布时间】:2011-08-05 01:54:49
【问题描述】:

我正在运行一个脚本,该脚本使用move_uploaded_file() 移动上传的文件。我已经这样做了数千次,但由于某种原因它不起作用。我已确认以下内容:

  1. <form> 使用 method="post" 并正确 enctype
  2. 从表单引用的正确文件
  3. 目录有权限777
  4. 所有memory_limitmax_execution_time 等都设置为超高设置以避免超时

基本上,下面的脚本只返回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>';
}

通过 php.ini hack 使用 1and1 托管:P

更新 1

我想补充一下,脚本的响应恰好在 60 秒后发生。

更新 2

我们可能会在这方面有所进展。只需print_r($_FILES),这是数组的结果:

Array ( 
    [image] => Array ( 
        [name] => P2120267.JPG 
        [type] => 
        [tmp_name] => 
        [error] => 1 
        [size] => 0 
    ) 
) 

所以这让我相信文件没有正确上传到服务器或其他什么?我查过了,发帖形式是&lt;form action="" method="post" enctype="multipart/form-data"&gt;。那么,据我所知,文件没有上传到服务器的临时区域?

更新 3

注意到上面数组中的[error] =&gt; 1。这显然取决于the filesize being larger than the upload_max_filesize。但是,当我将其设置为128M 时,60 秒后会出现白屏死机。我上传的文件是 2.5MB

这是我的 php.ini 文件:

register_globals=off
memory_limit = 128M 
max_execution_time=3600 
post_max_size = 128M
upload_max_filesize= 128M 

更新 4

根据上面的详细信息,我似乎收到了 WSOD,但正在上传图像。那么,如何停止WSOD呢?我在任何地方都找不到任何相关的错误。

更新 5 - 找到了!

很遗憾我没有给你们所有的代码。看起来它与这条线有关:

resizeImage($feedBurnerStatsSource, PHOTOMSGDIR.'temp/'.$time.'-tmp.jpg',$width,$height);

在以下代码中:

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

所以,WSOD 意味着它在没有消息的情况下死掉了。有什么想法吗?

【问题讨论】:

  • 尝试print_r($_FILES); 以查看完整的 FILES 数组。它可能包含错误代码。顺便说一句,您的错误消息实际上没有意义,因为您没有进行尺寸检查,并且 每个 错误条件都会返回“图像太大”
  • 文件名可能无效。那个文件密钥设置了吗?
  • 如果您没有收到任何错误消息,请尝试使用copy() 而不是move_uploaded_file()。在这种情况下也可以查看error.log
  • 那个target_path看起来特别奇怪,是error_reporting在吗?
  • $_FILES['image']['tmp_name']$target_path 上尝试echo else 块以检查路径是否正确。

标签: php image-processing file-upload image-manipulation gd


【解决方案1】:

只是为了验证post_max_filesize 是否设置为高电平?因为根据php.net:

如果 post 数据的大小大于 post_max_size,则 $_POST 和 $_FILES 超全局变量为空。 这可以通过多种方式进行跟踪,例如通过将$_GET 变量传递给处理数据的脚本,即&lt;form action="edit.php?processed=1"&gt;,然后检查是否设置了$_GET['processed']

需要考虑的事情。

有关更多信息,请参阅 this link 并向下滚动到 post_max_filesize 部分

更新

根据我的经验,如果您遇到 WSOD,通常会关闭 error_reportingdisplay_errors 或到达 memory_limit。在顶部的脚本中,我通常将 memory_limit 设置为 1024M 以验证这不是问题,然后打开 error_reportingdisplay_errors... 所以把它放在文件上传之前:

error_reporting(E_ALL); // or E_STRICT
ini_set("display_errors",1);
ini_set("memory_limit","1024M");

这通常会摆脱 WSOD 并让您和错误一起工作。

更新

您是否尝试在所有函数前面取消@ 错误抑制以查看它们是否产生特定错误?另外你的执行和输入超时是什么?并且您可以验证正在发送哪些标头吗? (确保是Content-Type=text/html;

【讨论】:

  • 是的,它设置为 256M。当我添加时,我会在 60 秒后得到“白屏死机”。
  • 完成了所有这些,仍然没有错误并且仍然得到相同的错误 = 1
  • 好的,图像看起来像是在 WSOD 之后不久上传的……但我需要阻止 WSOD。
  • 您是否尝试过取消所有函数前面的“@”错误抑制以查看它们是否产生了特定错误?另外你的执行和输入超时是多少?
【解决方案2】:

尝试将目标路径更改为非临时目录。

这里是一些更新后的分辨率:

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

...将此添加到处理上传的文件的开头。

【讨论】:

  • 它上传到的临时目录是我自己创建的,不是系统临时目录。
  • 您是否尝试添加以上内容?该设置适用于所有上传位置。
【解决方案3】:

[error] =&gt; 1表示上传的文件超过了php.ini中的upload_max_filesize指令。
http://www.php.net/manual/en/features.file-upload.errors.php

因此,您必须更改设置。

至于您在此处发布的 php.ini 文件,它不会影响您的 PHP。您必须将其移动到更合适的位置

【讨论】:

    【解决方案4】:

    上周我也遇到了同样的问题,只是因为我的服务器空间磁盘已满!我希望它会对某人有所帮助...

    【讨论】:

      【解决方案5】:

      我有同样的问题,但我想覆盖一个文件,所以我必须删除旧文件,然后它才能工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-19
        • 1970-01-01
        • 2013-01-17
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        • 2019-04-09
        • 2013-09-26
        相关资源
        最近更新 更多