【问题标题】:how can i make an image file downloadable from apache server using php?如何使用 php 制作可从 apache 服务器下载的图像文件?
【发布时间】:2020-03-20 19:08:36
【问题描述】:

不知道以前有没有问过这个问题,请见谅。

我使用 tinyPNG API 创建了一个图像压缩 Web 应用程序,它按预期工作 - 即用户可以上传图像文件并将其压缩并保存在服务器中。

然后我为用户创建了一个下载功能来下载/保存压缩图像。

点击下载链接会报错:

http://localhost/var/www/html/imgtest/uploads/67403938_10219921906785196_7063388762713096192_n_1574687362.jpg 找不到文件。

即使压缩图像在服务器中。

请帮忙!!!

我的代码:

require_once("vendor/autoload.php");

//makes PHP execute faster
set_time_limit(0);

//API key
\Tinify\setKey("API_KEY_HERE");

//Main code
if (isset($_POST['submit'])) {

    //supported image formats.
    $supported_image = array('image/jpg', 'image/jpeg', 'image/png');

    foreach($_FILES['images']['name'] as $key=>$val){

        $file_name = $_FILES['images']['name'][$key];

        // get file extension
        $ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

        // get filename without extension
        $filenamewithoutextension = pathinfo($file_name, PATHINFO_FILENAME);

        if (in_array($_FILES['images']['type'][$key], $supported_image)) {

            if (!file_exists(getcwd(). '/uploads')) {

                $oldmask = umask(0);

                mkdir(getcwd(). '/uploads', 0777, true);

                umask($oldmask);
            }

            $filename_to_store = $filenamewithoutextension. '_' .time(). '.' .$ext;
            move_uploaded_file($_FILES['images']['tmp_name'][$key], getcwd(). '/uploads/' .$filename_to_store);


            $compress_file = getcwd(). '/uploads/' .$filename_to_store;

            // optimize image using TinyPNG
            try {
                $source = \Tinify\fromFile(getcwd(). '/uploads/' .$filename_to_store);
                $source->toFile(getcwd(). '/uploads/' .$filename_to_store);

                //The code to show a modal for downloading the converted file.


            } catch(Exception $e) {
                echo $e->getMessage();
                exit;
            }
        }
    }

    echo "<a href=".$compress_file." download>Download</a>";
}

?>```

【问题讨论】:

    标签: php apache file server download


    【解决方案1】:

    您的代码中的问题是,您使用包含文件绝对路径的$compress_file 创建链接。

    /var/www/html/imgtest/uploads/67403938_10219921906785196_7063388762713096192_n_1574687362.jpg

    所以当链接被点击并在浏览器中解析 url 时,结果是

    http://localhost/var/www/html/imgtest/uploads/67403938_10219921906785196_7063388762713096192_n_1574687362.jpg.

    因此,如果 localhost 域指向 /var/www/html/imgtest/,那么服务器将在此路径中查找不存在的文件:

    /var/www/html/imgtest/var/www/html/imgtest/uploads/...

    当你生成压缩文件的路径时,你应该把它的 url 放到其他变量中,你不会在当前目录前面添加。

    $compress_file_url = '/uploads/' .$filename_to_store;
    $compress_file = getcwd(). $compress_file_url;
    

    那么你应该在创建链接时使用$compress_file_url 而不是$compress_file

    echo "<a href=\"$compress_file_url\" download>Download</a>";
    

    假设您的本地主机域指向/var/www/html/imgtest,这应该可以工作。如果您的 localhost 域指向 /var/www/html,您可能需要在 $compress_file_url 前面加上 /imgtest,然后才能使用它来创建链接。

    $compress_file_url = '/uploads/' .$filename_to_store;
    $compress_file = getcwd(). $compress_file_url;
    $compress_file_url = '/imgtest' . $compress_file_url;
    

    【讨论】:

    • 谢谢,它成功了:我改用$compress_file_url = '/imgtest' . $compress_file_url;。再次感谢您的及时帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多