Php ZipArchive未创建文件
这里怎么了?我刚刚看了一个创建档案和在其中添加文件的教程,但它不起作用……我正在使用PHP5.4。它甚至没有给我任何错误。谁能告诉我我做错了什么吗 以下是表格Php ZipArchive未创建文件,php,Php,这里怎么了?我刚刚看了一个创建档案和在其中添加文件的教程,但它不起作用……我正在使用PHP5.4。它甚至没有给我任何错误。谁能告诉我我做错了什么吗 以下是表格 if(isset($_POST['submit'])){ if(!empty($_FILES['files']['name'])){ $Zip = new ZipArchive(); $Zip->open('uploads.zip', ZIPARCHIVE::CREATE); $UploadFolder = "uploa
if(isset($_POST['submit'])){
if(!empty($_FILES['files']['name'])){
$Zip = new ZipArchive();
$Zip->open('uploads.zip', ZIPARCHIVE::CREATE);
$UploadFolder = "uploads/";
foreach($_FILES['files'] as $file){
$Zip->addFile($UploadFolder.$file);
}
$Zip->close();
}
else {
echo "no files selected";
}
}
选择要上载的文件
这些行没有任何意义
<form action="" method="POST" enctype="multipart/form-data">
<label>Select files to upload</label>
<input type="file" name="files">
<input type="submit" name="submit" value="Add to archieve">
</form>
在您发布的代码中,没有上载的文件被移动到uploads/
目录,并且通过$\u files[“files”]
元素循环-这是一个由各种值组成的关联数组,其中只有一个是实际的文件名-并将每个值作为文件添加到ZIP中,这是没有意义的。-你应该看报纸。很明显,您还不知道PHP如何处理文件上传,在尝试类似的事情之前,您应该先了解一下
一种解决方案是使用move\u uploads\u file
将上载的文件移动到uploads/
目录,但鉴于您实际上只是使用那里的文件将其添加到存档中,这一步是相当多余的;您可以直接从临时位置添加它。不过,首先您需要验证它,您可以使用is\u uploaded\u file
函数来验证它
$UploadFolder = "uploads/";
foreach($_FILES['files'] as $file){
$Zip->addFile($UploadFolder.$file);
}
发布您的表单,问题可能在那里。请阅读,特别注意描述
$\u文件的部分
super-global和每个条目的关联数组键。然后你会看到,在$\u文件['FILES']
上使用foreach
是一个非常糟糕的想法。我确实使用了移动上传的文件,但在这种情况下,我如何将文件添加到zip?这是一个内置的方法。。。为什么我的uploads.zip没有创建?我忘记了addFile
函数不会立即保存更改;它在保存之前等待close
调用。因此,在关闭存档之前取消临时文件的链接将导致问题。我已经相应地修改了我的答案顺便说一下,“仍然不工作”不是一个很好的问题描述。细节使调试更容易。
// Make sure the file is valid. (Security!)
if (is_uploaded_file($_FILES["files"]["tmp_name"])) {
// Add the file to the archive directly from it's
// temporary location. Pass the real name of the file
// as the second param, or the temporary name will be
// the one used inside the archive.
$Zip->addFile($_FILES["files"]["tmp_name"], $_FILES["files"]["name"]);
$Zip->close();
// Remove the temporary file. Always a good move when
// uploaded files are not moved to a new location.
// Make sure this happens AFTER the archive is closed.
unlink($_FILES["files"]["tmp_name"]);
}