PHP脚本压缩和下载文件夹的功能不起作用,但路径是有效的。

huangapple go评论69阅读模式
英文:

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?

&lt;?php
// Define the folder to be zipped
$folder_path = &#39;/CA11.2.4/logs/&#39;;




// Define the name of the zipped file with a timestamp
$zip_file_name = &#39;logs_&#39; . date(&#39;Ymd_His&#39;) . &#39;.zip&#39;;

// Create a new ZipArchive instance
$zip = new ZipArchive();

// Open the zip file for writing
if ($zip-&gt;open($zip_file_name, ZipArchive::CREATE) !== TRUE) {
    die (&#39;Could not create zip archive&#39;);
}

// 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-&gt;addFile($folder_path . $file, $file);
    }
}

// Close the directory handle
closedir($dir);

// Close the zip file
$zip-&gt;close();

// Set headers to force download the zip file
header(&#39;Content-Type: application/zip&#39;);
header(&#39;Content-Disposition: attachment; filename=&quot;&#39; . $zip_file_name . &#39;&quot;&#39;);
header(&#39;Content-Length: &#39; . 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(&#39;Location: &#39; . $_SERVER[&#39;HTTP_REFERER&#39;]); 
?&gt;

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.

&lt;?php
$path = &#39;/path/to/folder/file.txt&#39;;

if (file_exists($path)) {
    echo &#39;Path exists on server&#39;;
} else {
    echo &#39;Path does not exist on server&#39;;
}

答案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 = &#39;logs_&#39; . date(&#39;Ymd_His&#39;) . &#39;.zip&#39;;
$zip_location = &#39;/tmp/&#39; . $zip_file_name;

// Open the zip file for writing
if ($zip-&gt;open($zip_location, ZipArchive::CREATE) !== TRUE) {
    die (&#39;Could not create zip archive&#39;);
}

and then download it:

&lt;?php
$path = &#39;/tmp/file_name_where_previous_script_created_it.zip&#39;;

if (file_exists($path)) {
    echo &#39;Path exists on server&#39;;
} else {
    echo &#39;Path does not exist on server&#39;;
}

huangapple
  • 本文由 发表于 2023年2月24日 09:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定