1

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>
Good John
  • 33
  • 1
  • 9
  • What `var_dump($ftmpname);` outputs? – D4V1D Mar 26 '15 at 08:40
  • the var_dump($ftmpname); outputs the temp location – Good John Mar 26 '15 at 08:41
  • With the full path or just the filename? – D4V1D Mar 26 '15 at 08:41
  • full path of the file, when it is printed using print_r($ftmpname); you will see it gets Array ( [0] => /Applications/MAMP/tmp/php/phpGdxowS [1] => [2] => [3] => ) – Good John Mar 26 '15 at 08:46
  • So `$ftmpname` is an `array` and not a `string`? Does `ZipArchive->addFile()` can receive `arrays` as argument? – D4V1D Mar 26 '15 at 08:47
  • am not sure, thats why i tried to use foreach to see if it can help – Good John Mar 26 '15 at 08:51
  • Well, if you already `foreach()` on `$_FILES` and have an `array` when retrieving `$value['tmp_name']`, that means you have nested `arrays` so you need to `foreach()` on `$ftmpname` also. – D4V1D Mar 26 '15 at 08:53
  • Try: `foreach($ftmpname as $pathToFile) { $zip->addFile($pathToFile); }` just after you've assigned `$ftmpname`. – D4V1D Mar 26 '15 at 08:58

1 Answers1

2

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.

yergo
  • 4,761
  • 2
  • 19
  • 41
  • i have updated my code to this and it does not work yet, when i download, i get the same thing and when i direct it to the upload folder nothing comes – Good John Mar 26 '15 at 09:21
  • I need to update that answer as of $_FILE array may not look in way we're trying to work on it. Gimme few minutes. Also, please update your question with HTML snippet having part of form which is sending file to server. – yergo Mar 26 '15 at 09:23
  • i did the code above thanks to yergo but now i get an error when i unzip the file, it tells me unable to expand 1427362579.zip into upload. (Error 21 - is a directory) – Good John Mar 26 '15 at 09:43
  • Got it working on your example. Please check out edited answer. – yergo Mar 26 '15 at 09:43
  • what do i do to make it functional for multi-file form please – Good John Mar 26 '15 at 09:45
  • above answer works with your provided multi-file form on my environment. – yergo Mar 26 '15 at 09:47
  • thanks yergo, it works for a single file but when i upload more than one file it freezes and i get a file like this 1427363430.zip.fVutPN – Good John Mar 26 '15 at 09:54
  • have you tried to put few.. small files? Maybe you are exceeding some php/server limits. – yergo Mar 26 '15 at 10:00
  • setting up php/etc limits will need your research and maybe new question on SO. Start [here](http://stackoverflow.com/a/2184541/4615331). Glad i've helped. If this is all you need, you may want to accept my answer ;) – yergo Mar 26 '15 at 10:10
  • By cliccking "check" mark under upvote/downvote arrows. – yergo Mar 26 '15 at 10:36
  • how do i add restrictions to this script, e.g for it to accept image files only? – Good John Mar 27 '15 at 08:12