【问题标题】:Image comes to server as 0 bytes on upload PHP图片在上传 PHP 时以 0 字节的形式到达服务器
【发布时间】:2014-06-12 14:36:25
【问题描述】:

如果图像超过 2MB,则图像以 0 字节到达服务器。

我已经将我的 php.ini 文件更改为 64M 而不是默认的 2M。这发生在我的 WAMP 服务器和生产服务器上。

我的代码如下:

我的 HTML:

<div class="editunitimages">
        <form id="imageform" method="post" enctype="multipart/form-data" action='upload.php?id=<?php print $_GET['id']; ?>' >
            Upload image <input type="file" name="photoimg[]" id="photoimg" multiple />
        </form>

        <div id="dialog-confirm" title="Really Delete Image?" style="display:none;">
            <p><span class="ui-icon ui-icon-alert" style="float:left; margin:5px;">
            </span>Are you sure you want to delete this image?</p>
        </div>

        <div id='preview'>
        </div>

        <div id="unit_images">
            <?php
                $query = "SELECT id, image FROM images WHERE unit = ".$_GET['id']." ORDER BY position";
                $result = mysqli_query($con, $query);
                echo '<div class="yoxview"><ul id="sortable">';
                $i = 1;
                while ($row = mysqli_fetch_assoc($result))
                {
                    echo '<li id="listItem_'.$row['id'].'" class="ui-state-default"><div class="overlay"><a href="unit_images/'.$row['image'].'">
                    <img class="first_img" id="thumbnails" src="unit_images/thumbnails/'.$row['image'].
                    '" alt="'.$i.'" title="image '.$i.'" /></a><img class="sec_img" height="15px" width="15px" src="images/trash.png"/></div></li>';
                    $i++;
                }
                echo '</ul></div>';
            ?>
        </div>
    </div>

Javascript:

$('#photoimg').on('change', function()  
    { 
        var id = $('.editblock').find('#id').val();

        $("#preview").html('');
        $("#preview").html('<img src="images/loader.gif" alt="Uploading...."/>');
        $("#imageform").ajaxForm({
            target: '#preview'
        }).submit();
    });

还有我的upload.php文件

 <?php

function resize($newWidth, $targetFile, $originalFile) {
    $info = getimagesize($originalFile);
    list($width, $height) = getimagesize($originalFile);
    $mime = $info['mime'];

    switch ($mime) {
            case 'image/jpeg':
                    $image_create_func = 'imagecreatefromjpeg';
                    $image_save_func = 'imagejpeg';
                    $new_image_ext = 'jpg';
                    break;

            case 'image/png':
                    $image_create_func = 'imagecreatefrompng';
                    $image_save_func = 'imagepng';
                    $new_image_ext = 'png';
                    break;

            case 'image/gif':
                    $image_create_func = 'imagecreatefromgif';
                    $image_save_func = 'imagegif';
                    $new_image_ext = 'gif';
                    break;

            default: 
                    throw Exception('Unknown image type.');
    }

    $img = $image_create_func($targetFile);

    $src = imagecreatefromjpeg($originalFile);
    $newHeight = ($height / $width) * $newWidth;
    $tmp = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

    if (file_exists($targetFile)) {
        unlink($targetFile);
    }
    $image_save_func($tmp, $targetFile);
}

require_once('config/db.php');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$i = 0;
echo $_FILES['photoimg']['size'][0]."<br/>";
foreach ($_FILES['photoimg']['name'] as $f => $name) {
    $query = "SELECT MAX(id) AS id FROM images";
    $result = mysqli_query($con, $query);
    while ($row = mysqli_fetch_assoc($result))
    { $image_id = $row['id']; $image_id++; }

    $path = "unit_images/";

    $valid_formats = array("jpg", "jpeg", "png", "JPG", "JPEG", "PNG");
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
        {
            echo $size = $_FILES['photoimg']['size'][$f];

            if(strlen($name))
                {
                    list($txt, $ext) = explode(".", $name);
                    if(in_array($ext,$valid_formats))
                    {
                        if($size<((1024*1024)*10))
                        {
                            $actual_image_name = $_GET['id']."_".$txt.".".$ext;
                            echo $actual_image_name = $image_id.'_'.$actual_image_name;
                            echo $tmp = $_FILES['photoimg']['tmp_name'][$f];
                            if (move_uploaded_file($tmp, $path.$actual_image_name))
                            {
                                $file = $path.$actual_image_name;
                                $thumb = $path.'/thumbnails/'.$actual_image_name;
                                if (!copy($file, $thumb)) { echo 'Failed to copy.'; }
                                resize(80, $thumb, $file);

                                $query = "INSERT INTO images(unit, image) VALUES(".$_GET['id'].",'".$actual_image_name."')";
                                mysqli_query($con,$query);
                                //echo "<img src='unit_images/".$thumb."'  class='preview'>";
                            }
                            else
                                echo "\nCouldn't move file!";
                        }
                        else
                            echo "\nImage file size too large!";                    
                    }
                    else
                        echo "\nInvalid file format.."; 
                }

            else
                echo "\nPlease select image..!";

            //exit;
        }
}

mysqli_close($con);
?>
<script>
    //var id = $('.editblock').find('#id').val();
    //$('.editunitimages').load('classes/display_images.php?id='+<?php print $_GET['id']; ?>);
</script>

第一个 echo 返回为 0。它抛出的错误是Couldn't move file!。这告诉我从客户端到服务器正在发生一些事情,我不知道如何捕捉它。


编辑:我发现了以下内容:在 Windows 上的家庭 XAMPP 服务器上,我可以上传一个 1.7MB 的文件。在我的产品服务器上,我无法上传该文件。在 prod 服务器上大约需要 5 秒或更长时间。在我的 XAMPP serv 上,它需要不到 1 秒的时间。现在,两者都不能上传任何 2Meg 或更大的东西。我有一个 1MB 可以在两者上使用。产品上传时间约为 2 秒。是否有某种计时器阻止它工作?

另外,我注意到即使我在 C:\xamp\php\php.ini 中编辑我的php.ini 文件时,当我查看phpinfo() 时仍然无法更改它。产品服务器很好。帖子大小和上传大小最大值都设置为 6M。

【问题讨论】:

  • 我不知道 WAMP,但根据 stackoverflow.com/a/12049114/3571802 你不能直接编辑 WAMP php.ini 文件,必须通过他们的图标来完成
  • 好的,所以我很抱歉造成混乱。当我说 WAMP 时,我实际上是指我在 Windows 上使用 XAMPP。我以为他们是同一个。显然不是。
  • 没有问题。您的 Apache 配置中是否有与 LimitRequestBody 的一行?如果是这样,请尝试将其设置为 0(无限制)并重新启动 Apache
  • 我在哪里可以找到这个?
  • 不确定,尝试搜索C:\xampp\apache\conf\中的文件

标签: javascript php mysql image file-upload


【解决方案1】:

请在脚本顶部添加:-

error_reporting(-1);
ini_set('display_errors', 'On');

希望您在上传时会看到一些来自 PHP 的错误消息。

【讨论】:

  • 试过了。它没有帮助。
  • 你重启了webserver/php-fpm吗?
  • 是的,我确实重新启动了它。
  • 您能否按照我上面的帖子启用错误报告,然后尝试重新上传?如果问题与文件上传限制有关,则可能至少有来自 PHP 的警告消息
  • 好的,我做到了。我会在哪里看到警告/错误?没有什么对我很重要。
猜你喜欢
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 2014-07-16
  • 2015-10-19
  • 2016-10-31
  • 2014-01-10
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多