我正在尝试创建一个代码,通过文件上传控件从本地计算机中选择一个csv文件。 csv文件包含我要从服务器下载的文件的名称。当我按下提交按钮时,代码会读取csv文件并提取必须下载的文件的名称。现在代码应该创建一个zip文件,并将这些文件只添加到csv文件中提到的zip文件中。为此,我创建了以下代码:
<?php
if(isset($_POST['submit'])){
$filename=$_FILES['filename'];
$handle = fopen($_FILES['filename']['tmp_name'],"r");
$i=0;
while(($data = fgetcsv($handle,1000,","))!== FALSE){
$filenum[$i]=$data[0];
$i++;
}
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists("uploadedfiles/".$file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
print_r($zip);
//debug
echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
if (file_exists($destination) ){
// push to download the zip
echo "hiii";
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $destination . '"');
readfile($destination);
// remove zip file is exists in temp path
unlink($destination);
} else {
echo "error";
}
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
$result = create_zip($filenum,$_SERVER['DOCUMENT_ROOT'].'/misc/my-archive.zip');
}
?>
<form enctype='multipart/form-data' action='fileuploadproject.php' method='post'>
<input size='50' type='file' name='filename' id='filename'><br>
<input type='submit' name='submit' value='Download'>
</form>
我在语句print_r($ zip)时获取了添加到zip文件中的csv文件名的名称;已执行,但我找不到在服务器文件夹中创建的文件。所以我无法下载文件。为了获得csv文件中的文件名,应该做些什么来添加到zip文件中,然后将其下载到本地pc。请帮我找到我弄错的地方。感谢。
答案 0 :(得分:1)
这一行
echo "hiii";
将阻止要发送的header(),因为如果新加载的页面上有内容,则无法发送标头。此外,如果您之后添加内容,那么您的zip将被破坏。尝试不打印之前和之后的任何东西,它会起作用。
哦,正如pokeybit指出的那样,你取消了一个文件,然后验证它是否存在。这没有道理。而且,如果你没有传入
if(count($valid_files)) {
,$ zip-&gt; close();将无效,因为$ zip未声明。
答案 1 :(得分:0)
感谢dlegall pokeybit和所有其他试图帮助我解决问题的人。我在代码中做了很多更改,最后给我的代码如下:
<?php
if(isset($_POST['submit'])){
$filename=$_FILES['filename'];
$handle = fopen($_FILES['filename']['tmp_name'],"r");
$i=0;
while(($data = fgetcsv($handle,1000,","))!== FALSE){
$filenum[$i]=$data[0];
$i++;
}
$valid_files = array();
//if files were passed in...
if(is_array($filenum)) {
//cycle through each file
foreach($filenum as $file) {
//make sure the file exists
if(file_exists("uploadedfiles/".$file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive;
if ($zip->open("uploadedfiles/archive.zip", ZipArchive::CREATE)) {
foreach($valid_files as $file) {
$zip->addFile("uploadedfiles/".$file,$file);
}
$zip->close();
}
else
{
echo "Not Created";
}
if (file_exists("uploadedfiles/archive.zip") ){
// push to download the zip
$archive_file_name="uploadedfiles/archive.zip";
$archive_file="archive.zip";
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
unlink($archive_file_name);
exit;
} else {
echo "There was some problem in downloading the file";
}
}
else{
echo "<script>alert('No files in the CSV match the files on the server');</script>";
}
}
?>
<form enctype='multipart/form-data' action='fileuploadproject.php' method='post'>
Select the CSV file: <input style="color:white;background:coral;font-size:16px" size='50' type='file' name='filename' id='filename' required>
<input style="color:white;background:green;font-size:20px" type='submit' name='submit' value='Download'>
</form>