Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用php压缩其他网站目录中的文件_Php_Zip - Fatal编程技术网

如何用php压缩其他网站目录中的文件

如何用php压缩其他网站目录中的文件,php,zip,Php,Zip,我想创建一个zip文件,该文件的内容是从另一个网站检索到的。例如,我有一个文件: 我想以zip文件的形式下载file.txt。我尝试了file_get_contents()但它不工作 $content = file_get_contents(http://examples.com/path/file.txt); $zip = new ZipArchive; $res = $zip->open('config.zip', ZipArchive::CREATE); if ($res ===

我想创建一个zip文件,该文件的内容是从另一个网站检索到的。例如,我有一个文件:

我想以zip文件的形式下载
file.txt
。我尝试了
file_get_contents()但它不工作

$content = file_get_contents(http://examples.com/path/file.txt);
$zip = new ZipArchive;
$res = $zip->open('config.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFromString(file.txt, $content);
    $zip->close();
    echo 'OK';
} else {
    echo 'Failed';
}

我认为它将获得
file.txt的内容,并创建一个压缩的zip文件,但当我打开输出文件时,它不是(不完整的句子)。

如果只需要一个文件压缩,我宁愿使用
gzencode()

// dump & compress
$file = '/tmp/image.png.gz';
$content = file_get_contents('https://dummyimage.com/600x400/aff/bbb');
$compr = gzencode($content, 9);
file_put_contents($file, $compr);

// download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

请记住,可能存在各种文件权限/隔离问题。例如,默认情况下,Ubuntu为PHP进程隔离
/tmp
文件夹。如果希望PHP进程的/tmp文件夹是全局的,则必须将feature设置为
FALSE

如果您只需要1个文件压缩,我宁愿使用
gzencode()

// dump & compress
$file = '/tmp/image.png.gz';
$content = file_get_contents('https://dummyimage.com/600x400/aff/bbb');
$compr = gzencode($content, 9);
file_put_contents($file, $compr);

// download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

请记住,可能存在各种文件权限/隔离问题。例如,默认情况下,Ubuntu为PHP进程隔离
/tmp
文件夹。如果希望PHP进程的/tmp文件夹是全局的,则必须将feature设置为
FALSE

你试过在文件的路径上加引号吗?它毕竟是一根弦。此外,不要说“它不起作用”,而是尽量说得更具体一些。您是否收到
失败的
反馈?你有输出吗?等等。你有没有试着在文件的路径上加引号?它毕竟是一根弦。此外,不要说“它不起作用”,而是尽量说得更具体一些。您是否收到
失败的
反馈?你有输出吗?等