【问题标题】:Issue with file compressing with PHP使用 PHP 压缩文件的问题
【发布时间】:2019-12-18 11:32:58
【问题描述】:

我想使用 PHP 代码压缩图像,我有一个例子是 this

我已经尝试了一些代码,但是图像下载正确但没有压缩。

为此,我尝试了以下代码。

<div class="message">
    <?php
        if($_POST){
          	if ($error) {
            	?>
                <label class="error"><?php echo $error; ?></label>
                <?php
            }
        }
        ?>
</div>
<fieldset class="well">
    <legend>Upload Image:</legend>
    <form action="" name="myform" id="myform" method="post" enctype="multipart/form-data">
        <ul>
            <li>
                <label>Upload:</label>
                <input type="file" name="file" id="file"/>
            </li>
            <li>
                <input type="submit" name="submit" id="submit" class="submit btn-success"/>
            </li>
        </ul>
    </form>
</fieldset>
<?php
$name  = '';
$type  = '';
$size  = '';
$error = '';
function compress_image($source_url, $destination_url, $quality)
{
    $info = getimagesize($source_url);
    if ($info['mime'] == 'image/jpeg')
        $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif')
        $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png')
        $image = imagecreatefrompng($source_url);
    imagejpeg($image, $destination_url, $quality);
    return $destination_url;
}
if ($_POST) {
    if ($_FILES["file"]["error"] > 0) {
        $error = $_FILES["file"]["error"];
    } 
    else if (($_FILES["file"]["type"] == "image/gif") || 
    ($_FILES["file"]["type"] == "image/jpeg") || 
    ($_FILES["file"]["type"] == "image/png") || 
    ($_FILES["file"]["type"] == "image/pjpeg")) {

    $url = $_FILES["file"]["name"];

    $filename = compress_image($_FILES["file"]["tmp_name"], "image/$url", 80);
    move_uploaded_file($_FILES["file"]["tmp_name"], "image/$url");
    $location = "image/".$url;
    if (file_exists($location))
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($filename));
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filename));
        ob_clean();
        flush();
        readfile($location);
        exit;
    }
    }else {
        $error = "Uploaded image should be jpg or gif or png";
    }
}
?>

但是当我运行这个代码文件时,下载正常。

但它没有正确压缩。

显示错误

PHP 警告:file_get_contents(destination.jpg):打开流失败:没有这样的文件或目录

【问题讨论】:

  • @AksenP 你能解释一下这如何回答我的问题吗?
  • @MNJ, easy,您需要确保您的文件存在于该路径中。您的路径似乎不完整。否则,您的 $buffer 将是 null 并且进一步的代码将被破坏。 $_FILES["file"]["name"] 不是有效路径
  • @AksenP 抱歉,我刚刚更新了我的问题,根据我给出的示例,没有 $_FILES['file']['name'] 将其替换为 'destination.jpg'跨度>
  • @MNJ,没关系,file_get_contents() 无法通过此url 找到您的文件。例如,当我检查上传的文件时,我使用的是 file_get_contents($_FILES['file']['tmp_name'])。试试这个方法。

标签: php html file-get-contents


【解决方案1】:

您的路径无效。在使用file_get_contents() 之前,您应该始终检查您的文件是否真的存在于所选路径(目录)中。你可以在这里找到how to check existence

例如,在从客户端将文件上传到 PHP 后,我在处理任何文件时使用 file_get_contents($_FILES['file']['tmp_name'])

当然,我在做:

if(!@is_uploaded_file($_FILES['file']['tmp_name']) || $_FILES['file']['size'] == 0) return; 之前。

我建议你也做一些检查。否则,您的代码将损坏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2016-05-03
    相关资源
    最近更新 更多