doufei2328 2015-03-26 08:37
浏览 79
已采纳

上传多个文件并将其压缩到服务器的文件夹中

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. When I extract, I get a .cpgz file.

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

<?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" />&nbsp;
    <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>
  • 写回答

1条回答 默认 最新

  • dongyinpan9284 2015-03-26 08:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名