如何用php压缩其他网站目录中的文件
我想创建一个zip文件,该文件的内容是从另一个网站检索到的。例如,我有一个文件: 我想以zip文件的形式下载如何用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 ===
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
你试过在文件的路径上加引号吗?它毕竟是一根弦。此外,不要说“它不起作用”,而是尽量说得更具体一些。您是否收到失败的反馈?你有输出吗?等等。你有没有试着在文件的路径上加引号?它毕竟是一根弦。此外,不要说“它不起作用”,而是尽量说得更具体一些。您是否收到失败的反馈?你有输出吗?等