javascript - Multiple file upload and zip them on the fly failed -
i have created multiple file uploader using html & php. uploader functionality works fine while try create zip on fly in order add uploaded file zip file. in case, failed create zip , add uploaded file zip. don't know why it's not working.
please check scripts below , let me know if missed in advance:
html scripts:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>file upload progress bar</title> <script type="text/javascript"> // select file function styling input[type="file"] function select_file(){ document.getelementbyid('image').click(); return false; } </script> </head> <body> <div class="container"> <!--status message appear here--> <div class="status"></div> <!--image upload form--> <form class="pure-form" action="upload.php" enctype="multipart/form-data" method="post"> <div class="upload"> <a onclick="select_file()" class="pure-button">choose file</a> <input id="image" type="file" multiple="multiple" name="files[]" > </div> <!--image preview--> <img src="" style="display:none"> <input class="pure-button pure-button-primary" type="submit" value="upload!"> </form> </div> </body> </html>
php scripts:
header('content-type: application/json'); $path = 'uploads/'; // upload directory if (!file_exists('uploads')) { mkdir('uploads', 0777, true); } $max_file_size = 1024*10000; // 1024 byte= 1kb 1024*100 byte=100kb $count = 0; if ( $_server['request_method'] === 'post' ) { $zip_path = 'download.zip'; $zip = new ziparchive(); if ($zip->open($zip_path, ziparchive::overwrite) !== true) { die ("an error occurred creating zip file."); } foreach ($_files['files']['name'] $f => $name) { $filename = $_files['files']['name'][$f]; $filecontent = file_get_contents($_files["files"]["tmp_name"][$f]); $filetype = $_files['files']['type'][$f]; $filesize = $_files['files']['size'][$f]; $fileext = pathinfo($_files['files']['name'][$f], pathinfo_extension);; $zip->addfromstring($filename, $filecontent); //$zip->addfile('file_on_server.ext', 'second_file_name_within_archive.ext'); if ($_files['files']['error'][$f] == 4) { $status[] ='upload fail: unknown error occurred!'; continue; // skip file if error found } if ($_files['files']['error'][$f] == 0) { if ($_files['files']['size'][$f] > $max_file_size) { $status[] = "$name large!."; continue; // skip large files } else{ // no error found! move uploaded files if(move_uploaded_file($_files["files"]["tmp_name"][$f], $path.$name)){ $count++; // number of uploaded file $status[] = 'image uploaded!'; } else { $status[] = 'upload fail: unknown error occurred!'; } } } } $zip->close(); } else { $status[] = 'bad request!'; } echo json_encode(array('status' => $status));
it's not working download.zip file doesn't exist in directory. please create download.zip file or replace following code try again:
if ($zip->open($zip_path,ziparchive::overwrite) !== true) { die ("an error occurred creating zip file."); }
with
if ($zip->open($zip_path,ziparchive::create | ziparchive::overwrite) !== true) { die ("an error occurred creating zip file."); }
wiki
Comments
Post a Comment