英文:
The php script to zip and download the folder does not work but the path is valid
问题
下面是您提供的代码的翻译:
问题是,当文件夹中有大量文件时,下面的脚本下载了一个大小为0字节的zip文件。
我还使用了 if (file_exists="/folder/path/file.txt)
条件进行测试,脚本似乎能够找到文件。我做错了什么?
<?php
// 定义要压缩的文件夹路径
$folder_path = '/CA11.2.4/logs/';
// 使用时间戳定义压缩文件的名称
$zip_file_name = 'logs_' . date('Ymd_His') . '.zip';
// 创建一个新的ZipArchive实例
$zip = new ZipArchive();
// 打开zip文件以进行写入
if ($zip->open($zip_file_name, ZipArchive::CREATE) !== TRUE) {
die('无法创建zip存档');
}
// 将文件从文件夹添加到zip文件
$dir = opendir($folder_path);
while ($file = readdir($dir)) {
if (is_file($folder_path . $file)) {
$zip->addFile($folder_path . $file, $file);
}
}
// 关闭目录句柄
closedir($dir);
// 关闭zip文件
$zip->close();
// 设置标头以强制下载zip文件
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $zip_file_name . '"');
header('Content-Length: ' . filesize($zip_file_name));
// 将zip文件发送给客户端以进行下载
readfile($zip_file_name);
// 从服务器删除zip文件
//unlink($zip_file_name);
// 重定向回引用页面
//header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
最初我怀疑可能它没有检测到路径,所以我添加了这段代码并注释掉了其他代码以进行验证,它起作用了。路径被找到。
<?php
$path = '/path/to/folder/file.txt';
if (file_exists($path)) {
echo '服务器上存在路径';
} else {
echo '服务器上不存在路径';
}
英文:
The problem is that the script below is downloading a 0 bytes zip file when the folder has lots of files.
I have also tested this with if (file_exists="/folder/path/file.txt) clause and the script seems to be ok with finding the file. What am I doing wrong?
<?php
// Define the folder to be zipped
$folder_path = '/CA11.2.4/logs/';
// Define the name of the zipped file with a timestamp
$zip_file_name = 'logs_' . date('Ymd_His') . '.zip';
// Create a new ZipArchive instance
$zip = new ZipArchive();
// Open the zip file for writing
if ($zip->open($zip_file_name, ZipArchive::CREATE) !== TRUE) {
die ('Could not create zip archive');
}
// Add the files from the folder to the zip file
$dir = opendir($folder_path);
while ($file = readdir($dir)) {
if (is_file($folder_path . $file)) {
$zip->addFile($folder_path . $file, $file);
}
}
// Close the directory handle
closedir($dir);
// Close the zip file
$zip->close();
// Set headers to force download the zip file
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $zip_file_name . '"');
header('Content-Length: ' . filesize($zip_file_name));
// Send the zip file to the client for download
readfile($zip_file_name);
// Delete the zip file from the server
//unlink($zip_file_name);
// Redirect back to the referring page
//header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
I had initially suspected maybe its not detecting the path so I added this snippet and commented out other code to verify and it worked. The path was found.
<?php
$path = '/path/to/folder/file.txt';
if (file_exists($path)) {
echo 'Path exists on server';
} else {
echo 'Path does not exist on server';
}
答案1
得分: 0
似乎问题最可能是路径问题,而不是压缩问题。根据我的经验,在PHP中,最好始终使用绝对路径,而不要假设两个独立的文件会知道/了解它们相对于X的位置。
// 定义带有时间戳的压缩文件名
$zip_file_name = 'logs_' . date('Ymd_His') . '.zip';
$zip_location = '/tmp/' . $zip_file_name;
// 打开用于写入的zip文件
if ($zip->open($zip_location, ZipArchive::CREATE) !== TRUE) {
die('无法创建zip存档');
}
然后下载它:
<?php
$path = '/tmp/之前脚本创建的文件名.zip';
if (file_exists($path)) {
echo '服务器上存在路径';
} else {
echo '服务器上不存在路径';
}
英文:
It seems to me, that the problem is most likely a path problem, not Zip problem. in PHP, in my experience, it's always better to use absolute paths and not assume that 2 separate files will know/understand where they are located related to X.
// Define the name of the zipped file with a timestamp
$zip_file_name = 'logs_' . date('Ymd_His') . '.zip';
$zip_location = '/tmp/' . $zip_file_name;
// Open the zip file for writing
if ($zip->open($zip_location, ZipArchive::CREATE) !== TRUE) {
die ('Could not create zip archive');
}
and then download it:
<?php
$path = '/tmp/file_name_where_previous_script_created_it.zip';
if (file_exists($path)) {
echo 'Path exists on server';
} else {
echo 'Path does not exist on server';
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论