dreamice2013 2015-12-08 12:52
浏览 69

使用ZipArhive将文件压缩为URL

I have a WordPress site where users are gonna be able to download one, or multiple pdf files. So far I just coded without using any WordPress functionalities and with having the files on my computer. But in the future, I want to be able to add a file using Advanced Custom Fields (or something similar) which means that I won't have the files in a folder and will have to use URL's instead. At least I think? And when I use the same code, but with URL's (see below), it doesn't work.

So how can I create a zip file from a URL using ZipArchive?

<?php
    if(isset($_POST['createzip']))
    {
        $files = $_POST['files'];
        $zipname = time().".zip"; // Zip name
        $zip = new ZipArchive(); // Load zip library
        $zip->open($zipname, ZipArchive::CREATE);
        foreach ($files as $file) {
            $zip->addFile($file);
        }
        $zip->close();
    // push to download the zip
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename='.$zipname);
        header('Content-Length: ' . filesize($zipname));
        readfile($zipname);
    }
?>

<h1> hej här kan du zippa lite filer</h1>

<form name="zips" method="post">
    <input type="checkbox" name="files" value="http://www.unstraight.org/wp-content/uploads/2015/08/seger3.jpg">
    <p>Seger</p>
    <input type="checkbox" name="files" value="http://www.unstraight.org/wp-content/uploads/dlm_uploads/2015/12/Ovningar-medelsvara.pdf">
    <p>Övningar </p>
    <input type="checkbox" name="files" value="http://www.unstraight.org/wp-content/uploads/2015/07/User-Agreement-.pdf">
    <p>Medlemskort </p>
    <input type="checkbox" name="files[]" value="men.pdf">
    <p>Män och Jämställdhet </p>
    <input type="submit" name="createzip" value="Download as ZIP">
</form>
  • 写回答

1条回答

  • doupao6698 2015-12-08 13:55
    关注

    You can retrieve the document from the URL and then use ZipArchive::addFromString.

    Documentation: http://php.net/manual/en/ziparchive.addfromstring.php

    The code might look something like this:

    foreach ($files as $file) {
        if(preg_match('/^https?\:/', $file)) {
    
            // Looks like a URL
    
            // Generate a file name for including in the zip
            $url_components = explode('/', $file);
            $file_name = array_pop($url_components);
    
            // Make sure we only have safe characters in the filename
            $file_name = preg_replace('/[^A-z0-9_\.-]/', '', $file_name);
    
            // If all else fails, default to a random filename
            if(empty($file_name)) $file_name = time() . rand(10000, 99999);
    
            // Make sure we have a .pdf extension
            if(!preg_match('/\.pdf$/', $file_name)) $file_name .= '.pdf';
    
            // Download file
            $ch = curl_init($file);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
            $file_content = curl_exec($ch);
            curl_close($ch);
    
            // Add to zip
            $zip->addFromString($file_name, $file_content);
    
        } else {
    
            // Looks like a local file
            $zip->addFile($file);
    
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误