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.

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

报告相同问题?

悬赏问题

  • ¥15 stm32 函数调用问题
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏