[英]uploading multiple files and zip them in a folder in the server
This is the code I am using to upload file and then it seam not to work. 这是我用来上传文件的代码,然后它无法正常工作。 I have tried forcing it to download and I get a blank zip file.
我试图强迫它下载,但得到一个空白的zip文件。 When I extract, I get a .cpgz file.
提取时,我得到一个.cpgz文件。
The zip file seam to be created, but the uploaded files are not getting inside the zip file i have updated the code, this is how the entire code look like 待创建的zip文件接缝,但是上传的文件不在zip文件中,我已经更新了代码,这就是整个代码的样子
<?php
if(isset($_POST['createpdf']))
{
$file_folder = "files/"; // folder to load files
$zip = new ZipArchive(); // Load zip library
$zip_name = "upload/".time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach($_FILES['file']['tmp_name'] as $k => $filesuploaded) {
$fname = $_FILES['name'][$k];
$ftmpname = $filesuploaded;
$zip->addFromString(basename($fname),
file_get_contents($ftmpname));
}
$zip->close(); }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload As Zip</title>
</head>
<body>
<center><h1>Create Zip</h1></center>
<form name="zips" method="post" action="" enctype="multipart/form-data">
<?php if(!empty($error)) { ?>
<p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; width:588px; margin:0 auto 10px;"><?php echo $error; ?> </p>
<?php } ?>
<table width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid;">
<tr>
<td width="33" align="center">*</td>
<td width="117" align="center">File Type</td>
<td width="382">File Name</td>
</tr>
<tr>
<td align="center"><input type="file" name="file[]" /></td>
<td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
<td>flowers.jpg</td>
</tr>
<tr>
<td align="center"><input type="file" name="file[]" /></td>
<td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
<td>fun.jpg</td>
</tr>
<tr>
<td align="center"><input type="file" name="file[]" /></td>
<td align="center"><img src="files/doc.png" title="Document" width="16" height="16" /></td>
<td>9lessons.docx</td>
</tr>
<tr>
<td align="center"><input type="file" name="file[]" /></td>
<td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td>
<td>9lessons.pdf</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" name="createpdf" style="border:0px; background-color:#800040; color:#FFF; padding:10px; cursor:pointer; font-weight:bold; border-radius:5px;" value="Upload" />
<input type="reset" name="reset" style="border:0px; background-color:#D3D3D3; color:#000; font-weight:bold; padding:10px; cursor:pointer; border-radius:5px;" value="Reset" />
</td>
</tr>
</table>
</form>
</body>
</html>
Got it working in this form: 它以这种形式工作:
<?php
$error = '';
if(isset($_POST['createpdf'])) {
//$file_folder = "files/"; // folder to load files
$zip = new ZipArchive(); // Load zip library
$zip_name = "upload/" . time() . ".zip"; // Zip name
if($zip->open($zip_name, ZipArchive::CREATE) !== TRUE) {
//Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
$files = $_FILES['file']; // current 'file' field posted from HTML page
if(is_array($files['tmp_name'])) {
// multi file form
foreach($files['tmp_name'] as $k => $value) {
if($value == '') { // not empty field
continue;
}
$zip->addFromString($files['name'][$k], file_get_contents($value));
}
} elseif($files['tmp_name'] != '') { // not empty field
// single file form
$zip->addFromString($files['name'], file_get_contents($files['tmp_name']));
}
$zip->close();
}
?>
Should work as for single-file form as for multi-file form. 应该像单文件格式一样工作。 Tried to comment our useful tips.
试图评论我们的有用提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.