这是我用来上传文件的代码然后它无法正常工作。我试图强制下载,我得到一个空白的zip文件。当我提取时,我得到一个.cpgz文件。
要创建的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>
答案 0 :(得分:2)
以这种形式工作:
<?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();
}
?>
应该像单文件格式一样用于多文件格式。试图评论我们的有用提示。