dsq30861 2013-05-11 12:28
浏览 56
已采纳

无法使用PHP创建zip文件

I've created the following code to create a zip file. I'm pulling a list of files from the database depending on the $job_number (which I'm getting from the global $_GET array) and then trying to add them to the zip file.

That part is working fine. It's pulling the list of files from the database, as I can see by echoing or dumping with print_r the results.

The only problem is that the .zip file isn't being created at all. I can't see where I've gone wrong.

$sth = $conn->prepare('SELECT `dp_filename` FROM damage_pics WHERE dp_job_number = :job_number');
$sth->bindParam(':job_number', $job_number);
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();

$zip = new ZipArchive();
$zip->open('example5.zip', ZipArchive::CREATE);
$result = $sth->fetchAll();


echo '<pre>';
print_r($result);

foreach ($result as $file)
{
    // just echoing for testing purposes to see if file name is created correctly.
    $image = $file['dp_filename'];
    echo $image . "<br />";
    $zip->addFile("uploads/{$image}");
}

$zip->close();
  • 写回答

1条回答 默认 最新

  • duannima8347 2013-05-11 13:01
    关注

    Sounds like your PHP script is being run with insufficient permissions to write to the destination directory. I did some experimenting, and in such a situation the $zip->open() call would silently fail with no indication of what went wrong.

    You could put the following code at the top to determine if this is indeed the problem:

    $fp = fopen('example5.zip', 'w');
    if ($fp === FALSE) { die("Cannot open example5.zip for writing"); }
    fclose($fp);
    

    To fix the issue, I would suggest using an absolute path and filename (such as /tmp/example5.zip) and then make sure that the destination directory is writable by the user that's executing the script (likely whatever user is running the HTTP server software, such as www-data or httpd, depending on the OS and distribution).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目