我遇到了这个错误:
[2013年7月23日16:26:15 America / New_York] PHP警告: readfile(Resumes.zip):无法打开流:没有这样的文件或 第24行/home/mcaplace/public_html/download.php中的目录
这是代码:
<?php
/*foreach($_POST['files'] as $check) {
echo $check;}*/
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.$files,$files);
/*if(file_exists($file_path.$files)) //." ".$files."<br>";
echo "yes";*/
}
$zip->close();
//then send the headers to foce download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile($archive_file_name);
exit;
}
//If you are passing the file names to thae array directly use the following method
$file_names = $_POST['files'];
//if you are getting the file name from database means use the following method
//Include DB connection
$archive_file_name='Resumes.zip';
$file_path=$_SERVER['DOCUMENT_ROOT']."/resumes/";
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
?>
答案 0 :(得分:1)
你有几个错误 首先关闭ZIPARCHIVE :: CREATE是不正确的应该是ZipArchive :: CREATE或ZipArchive :: OVERWRITE(如果不删除zip文件则覆盖)。
接下来你的路径错了。您没有告诉readfile()文件的绝对路径。因此,您需要提供保存zip文件的位置和读取位置的绝对路径。
此外,我发现网站的根文件夹是可写的,这是不可能的。所以我将zip移动到以下代码的简历文件夹中。我倾向于远离$ _SERVER ['DOCUMENT_ROOT']我自己并使用realpath()函数。
最后,您必须确保将zip文件创建的文件夹中包含777权限。
这是完整的代码更改
<?php
/*foreach($_POST['files'] as $check) {
echo $check;}*/
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($file_path.$archive_file_name, ZipArchive::OVERWRITE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.$files,$files);
/*if(file_exists($file_path.$files)) //." ".$files."<br>";
echo "yes";*/
}
$zip->close();
//then send the headers to foce download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile($file_path.$archive_file_name);
exit;
}
//If you are passing the file names to thae array directly use the following method
$file_names = array('test.txt');
//if you are getting the file name from database means use the following method
//Include DB connection
$archive_file_name='Resumes.zip';
$file_path=$_SERVER['DOCUMENT_ROOT']."/resumes/";
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
?>
答案 1 :(得分:0)
我在PHP 7.0中遇到了类似的问题,可以使用OHCC的解决方案来修复它:http://php.net/manual/de/ziparchive.open.php#118273
他建议同时使用标志OVERWRITE和CREATE。
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<configuration>
<exclusions>
<!-- exclude the artifact -->
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals><goal>proguard</goal></goals>
</execution>
<execution>
<id>obfuscate-uber</id>
<phase>package</phase>
<goals><goal>proguard</goal></goals>
<configuration>
<injar>${project.build.finalName}-uber.jar</injar>
<outjar>${project.build.finalName}-uber.jar</outjar>
<!-- filter out unwanted parts -->
<inFilter>!META-INF/versions/9/**.class</inFilter>
</configuration>
</execution>
</executions>
</plugin>
这解决了我的错误。