【问题标题】:Create a file in a zip archive using stream wrapper使用流包装器在 zip 存档中创建文件
【发布时间】:2009-11-13 19:24:28
【问题描述】:

我想在 PHP 中使用 zip 流包装器来创建 zip 文件或在 zip 存档中添加文件。这是我的例子:

<?php
echo file_get_contents('zip://file.zip#existing_file.txt');
file_put_contents('zip://file.zip#new_file.txt', 'Trying to put some stuff here...');
echo file_get_contents('zip://file.zip#new_file.txt');

这段代码让我得到这个输出:

$ php test.php 
Hey, I'm an existing text in a file in a zip archive !

Warning: file_put_contents(zip://file.zip#new_file.txt): failed to open stream: operation failed in /home/sylvain/test.php on line 7

Warning: file_get_contents(zip://file.zip#new_file.txt): failed to open stream: operation failed in /home/sylvain/test.php on line 9

当 zip 文件不存在时,我得到同样的错误。

这里是文件权限:

$ ls -la
total 24
drwxrwxrwx   2 sylvain sylvain  4096 2009-11-13 14:44 .
drwxr-xr-x 134 sylvain sylvain 12288 2009-11-13 14:44 ..
-rwxrwxrwx   1 sylvain sylvain   236 2009-11-13 14:44 file.zip
-rwxrwxrwx   1 sylvain sylvain   268 2009-11-13 14:44 test.php

是否可以在不创建新的流包装器的情况下做我想做的事情?

【问题讨论】:

    标签: php zip


    【解决方案1】:

    zip:// 流包装器不支持写入:http://php.net/manual/en/wrappers.compression.php

    此外,Zip 存档不能写入流中,因为 CRC 位于压缩数据之前

    【讨论】:

    【解决方案2】:

    嘿,您厌倦了将您的文件夹 chmod 到 ZIP 文件所在的 777 吗?

    编辑:看起来您的文件不是 777 和您的文件夹。我可能是错的,刚刚醒来。 chmod 777 文件夹本身,如果这不起作用,请告诉我。

    【讨论】:

    • 是的,我试过了,我编辑了我的问题,包括 ls -la 命令的输出
    • 文件在777,你可以看到用户、组和所有用户的rwx。 "." 也一样
    猜你喜欢
    • 2017-02-07
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 2015-11-17
    • 2013-06-18
    • 1970-01-01
    相关资源
    最近更新 更多