php 创建 zip 文件(来自 post 附件 wordpress)

     2023-03-06     21

关键词:

【中文标题】php 创建 zip 文件(来自 post 附件 wordpress)【英文标题】:Php create zip file (from post attachments wordpress) 【发布时间】:2012-03-28 11:19:46 【问题描述】:

我正在尝试从 wordpress 中的帖子附件创建一个 zip 文件。

我已经尝试了以下两种方法 - 但它没有任何结果(没有错误消息,没有创建文件) - 我做错了什么(再次......)

我不认为那些是 wordpress 中的帖子附件的事实与它有任何关系 - 因为这些方法对于普通文件也失败了。为什么? --> 看看答案。

$files_to_zip = array();// create files array
    //run a query
    $post_id = get_the_id();
    $args = array(
        'post_type' => 'attachment',
        'numberposts' => null,
        'post_status' => null,
        'post_parent' => $post_id
    );

    $attachments = get_posts($args);
    if ($attachments) 
foreach ($attachments as $attachment) 
        $files_to_zip [] = wp_get_attachment_url( $attachment->ID ); // populate files array
        
    
    print_r($files_to_zip);
    $zip = new ZipArchive;
    $zip->open('file.zip', ZipArchive::CREATE);
    foreach ($files_to_zip as $file) 
      $zip->addFile($file);
    
    $zip->close();

还有这个方法:

$files_to_zip = array(); // create array
//run a query
$post_id = get_the_id();
$args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post_id
);
$zip = new ZipArchive;
$zip->open('file.zip', ZipArchive::CREATE);
$attachments = get_posts($args);
if ($attachments) 
    foreach ($attachments as $attachment) 
     $zip->addFile(wp_get_attachment_url( $attachment->ID ));
    


print_r($files_to_zip);// debug - return file names OK

$zip->close();

这两种方法都没有返回任何内容.. 任何见解将不胜感激。

EDIT I - 数组 $files_to_zip 的示例 print_r

print_r($files_to_zip);

Array ( 
[0] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-62316IMAG0659.jpg 
[2] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_IGP0255.jpg
[3] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_IZTP0635.jpg
[4] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_ITG035t5.jpg
[5] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_IRTT7375.jpg )

..通过使用 get_attached_file() 它将产生 real 路径(在某些时候我怀疑也许 php 无法通过 HTTP 创建 zip - 这就是简短的答案。请参阅下面的长篇.)

【问题讨论】:

没有称为 create_zip() 的原生 PHP 函数 - 请显示此函数的代码。另外,请显示var_dump($files_to_zip)的结果 查看我对示例数组的编辑。 【参考方案1】:

好的 - 我会在这里回答我自己的问题..

我已经证实了我自己的怀疑 - PHP 在通过 HTTP 传递时无法创建 ZIP - 所以我们需要一个 PATH 而不是 URL ...

因此,例如在 Wordpress 的情况下,需要使用 get_attached_file() 来生成真实路径..

Array ( 
[0] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-62316IMAG0659.jpg 
[2] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_IGP0255.jpg
[3] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_IZTP0635.jpg
[4] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_ITG035t5.jpg
[5] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_IRTT7375.jpg )

(感谢@DaveRandom 对看到 var_dump 数组的评论——我实际上已经看过很多次了,但直到有人特别要求查看它之前我并没有太在意。)

然后它让我想起了很久以前我在 gdlib 中遇到的另一个问题——关于 PHP 流函数、创建文件和 HTTP。 例如像 gdlib 这样的图像库,或者 pdf 动态创建它们都在 HTTP 上失败。

【讨论】:

使用来自 Textfile 的文件创建 7-Zip 存档 - Powershell

】使用来自Textfile的文件创建7-Zip存档-Powershell【英文标题】:Create7-ZipArchivewithFilesfromTextfile-Powershell【发布时间】:2016-02-2610:51:05【问题描述】:我正在编写一个小型Powershell应用程序,供我们的支持者存档文件。但现在我因为7zi... 查看详情

php创建没有zip文件路径的zip

】php创建没有zip文件路径的zip【英文标题】:phpcreatingzipswithoutpathtofilesinsidethezip【发布时间】:2011-04-2822:20:43【问题描述】:我正在尝试使用php创建一个zip文件(它确实如此-取自此页面-http://davidwalsh.name/create-zip-php),但是在zip... 查看详情

来自 zip 的文件响应

...问题描述】:这就是我想要做的:我已经使用ZipArchive类创建了zip存档,其中包含多个文件,我现在需要做的是,打开存档,读取单个文件并下载或在浏览器中打开它。由于我使用的是symfony2框架,如果它是一个常规文件,我可以... 查看详情

无法创建 zip 文件 (php)

】无法创建zip文件(php)【英文标题】:Can\'tcreatezipfile(php)【发布时间】:2016-04-1502:34:35【问题描述】:所以我在互联网上找到了这个可以将文件放入zip的类。我有一个表单,可以将图片上传到文件夹中,然后我尝试循环进入这些... 查看详情

如何使用 php 读取 zip 文件的创建日期

】如何使用php读取zip文件的创建日期【英文标题】:Howtoreadzipfilecreationdatewithphp【发布时间】:2018-04-1710:15:40【问题描述】:我正在开发大型业务应用程序,我面临创建(多个文件和目录)下载按钮的问题,该按钮会在这些目录... 查看详情

PHP Zip Archive 偶尔创建多个文件

】PHPZipArchive偶尔创建多个文件【英文标题】:PHPZipArchivesporadicallycreatingmultiplefiles【发布时间】:2011-01-1119:49:41【问题描述】:我有一个PHP脚本(在Apache2.2上运行PHP5.2),它创建了一个文件的zip存档供用户下载。一切似乎都很好... 查看详情

来自不同目录问题的 PHP ZipArchive::AddFile

...储在C:\\pdfgenerator(不是我的www目录)中,而zip文件本身创建并存储在C:\\Progr 查看详情

php创建zip文件(代码片段)

查看详情

使用 ruby​​zip 在嵌套模型中下载回形针附件

...提交的文件带有附件(使用回形针)。我希望作业用户(创建者)能够下载属于特定作业的文件(提交)。我的路线结构如下:resour 查看详情

在文件名上使用 ReceivedTime 保存来自 Outlook 的附件

...【发布时间】:2016-05-0316:16:49【问题描述】:我正在尝试创建一个可以保存电子邮件附件的宏。我目前遇到的问题是我希望宏在它保存的文件名上添加电子邮件的ReceivedTime(即:文件TESTSHEET.xls于2016年1月1日上午3: 查看详情

从字节创建 zip 文件

】从字节创建zip文件【英文标题】:Creatingzipfilefrombyte【发布时间】:2016-10-1011:51:29【问题描述】:我正在使用JSZip从客户端发送zip文件的字节字符串,并且需要在服务器端将其转换回zip。我试过的代码不起作用。b=bytearray()b.exten... 查看详情

在 SDCard 上创建包含一些文件的 zip 文件

】在SDCard上创建包含一些文件的zip文件【英文标题】:CreatingazipfilewithsomefilesonSDCard【发布时间】:2011-08-1223:08:21【问题描述】:当我几天前发布一个问题时,我意识到股票电子邮件应用程序无法在附件中发送多个文件:https://***... 查看详情

使用 PHP 从 sendmail 管道创建目录并写入文件以进行编程

】使用PHP从sendmail管道创建目录并写入文件以进行编程【英文标题】:CreatedirectoriesandwritefilesusingPHPfromasendmailpipetoprogram【发布时间】:2011-05-1104:38:00【问题描述】:我有一个从管道读取电子邮件(带有附件)的脚本,我正在尝试... 查看详情

PHP:使用 ZipArchive 创建 zip 文件时出现错误 (500)

】PHP:使用ZipArchive创建zip文件时出现错误(500)【英文标题】:PHP:Error(500)whencreatingazipfilewithZipArchive【发布时间】:2016-01-1117:18:20【问题描述】:我正在使用phpsZipArchive类来创建目录的zip文件。该代码适用于几乎所有目录,除了两... 查看详情

ZipStream 在 PHP 中动态创建的 zip 文件不会在 OSX 中打开

】ZipStream在PHP中动态创建的zip文件不会在OSX中打开【英文标题】:DynamicallycreatedzipfilesbyZipStreaminPHPwon\'topeninOSX【发布时间】:2011-04-0621:31:35【问题描述】:我有一个包含大量媒体文件的PHP站点,用户需要能够一次下载多个.zip文... 查看详情

在 PHP 中从 AWS S3 中的文件创建 zip 的最佳方法? [关闭]

】在PHP中从AWSS3中的文件创建zip的最佳方法?[关闭]【英文标题】:BestwaytocreatezipfromfilesinAWSS3inPHP?[closed]【发布时间】:2012-12-1710:45:57【问题描述】:我销售在S3中托管的歌曲。人们选择它们并可以下载,但如果他们购买整张CD,... 查看详情

PHP - 发送带附件的电子邮件不显示邮件内容

...gecontent【发布时间】:2015-08-2908:39:09【问题描述】:尝试创建一个脚本,我可以在其中发送带有附件的电子邮件。一切正常,除了当我不在电子邮件中添加文件时,我仍然可以看到一个0B且没有名称的附件。if(isset($_POST["my_send"]))... 查看详情

PHP 即时创建和下载 ZIP

】PHP即时创建和下载ZIP【英文标题】:PHPcreateanddownloadZIPonthefly【发布时间】:2014-10-2802:31:26【问题描述】:我正在创建一个包含多个脚本的ZIP文件(例如:test.php、functions.js和style.css)。脚本运行良好,唯一的问题是ZIP文件被放... 查看详情