Guest User

Untitled

a guest
Sep 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. <?php
  2. // Code à remanier
  3. $chemin = '/Users/submarine/Sites/lab/intranet/photos/';
  4. $error = '';
  5. set_time_limit(0);
  6. $errors = array(
  7. UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
  8. UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
  9. UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
  10. UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
  11. UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
  12. UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
  13. UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.'
  14. );
  15.  
  16. if( isset($_POST['submit']) ){
  17. if( isset($_FILES['zip']) ){
  18. if( !$_FILES['zip']['error'] ){
  19. if( is_uploaded_file($_FILES['zip']['tmp_name']) ){
  20. // c'est un fichier zip ?
  21. if( preg_match('#\.zip$#i', $_FILES['zip']['name']) ){
  22. $zip = new ZipArchive();
  23. if( $zip->open($_FILES['zip']['tmp_name']) ){
  24. // boucle sur tous les fichiers dans l'archive
  25. for( $i = 0; $i < $zip->numFiles; $i++){
  26. $entry = $zip->statIndex($i);
  27. // une image?
  28. if( $entry['size'] > 0 && preg_match('#\.(jpg|gif|png)$#i', $entry['name'] ) ){
  29. $pos = strrpos($entry['name'],'_')+1;
  30. $nom = substr($entry['name'], $pos,strlen($entry['name'])-$pos);
  31. $file = $zip->getFromIndex($i); // Récupère le contenu du fichier
  32. if( $file ) {
  33. $fh = fopen($chemin.$nom, 'wb') or die("can't open file");
  34. fwrite($fh, $file); // Ecriture du fichier dans le répertoire de destination
  35. fclose($fh);
  36. }
  37. }
  38. }
  39. $zip->close();
  40. exit();
  41. }
  42. }
  43. else{
  44. echo "not a zip resource";
  45. exit();
  46. }
  47. }
  48. }
  49. else{
  50. $error = $errors[$_FILES['zip']['error']];
  51. }
  52. }
  53. }
  54. ?>
  55. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  56. <html>
  57. <head>
  58. <title>Zip to Thumbnails :p</title>
  59. </head>
  60. <body>
  61. <?php if( !empty($error) ){ ?>
  62. <h3 style="color: red;"><?php echo $error?></h3>
  63. <?php } ?>
  64. <form method="post" enctype="multipart/form-data">
  65. <input type="file" name="zip" />
  66. <input type="submit" name="submit" value="Envoyer" />
  67. </form>
  68. </body>
  69. </html>
Add Comment
Please, Sign In to add comment