Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Code à remanier
- $chemin = '/Users/submarine/Sites/lab/intranet/photos/';
- $error = '';
- set_time_limit(0);
- $errors = array(
- UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
- UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
- UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
- UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
- UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
- UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
- UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.'
- );
- if( isset($_POST['submit']) ){
- if( isset($_FILES['zip']) ){
- if( !$_FILES['zip']['error'] ){
- if( is_uploaded_file($_FILES['zip']['tmp_name']) ){
- // c'est un fichier zip ?
- if( preg_match('#\.zip$#i', $_FILES['zip']['name']) ){
- $zip = new ZipArchive();
- if( $zip->open($_FILES['zip']['tmp_name']) ){
- // boucle sur tous les fichiers dans l'archive
- for( $i = 0; $i < $zip->numFiles; $i++){
- $entry = $zip->statIndex($i);
- // une image?
- if( $entry['size'] > 0 && preg_match('#\.(jpg|gif|png)$#i', $entry['name'] ) ){
- $pos = strrpos($entry['name'],'_')+1;
- $nom = substr($entry['name'], $pos,strlen($entry['name'])-$pos);
- $file = $zip->getFromIndex($i); // Récupère le contenu du fichier
- if( $file ) {
- $fh = fopen($chemin.$nom, 'wb') or die("can't open file");
- fwrite($fh, $file); // Ecriture du fichier dans le répertoire de destination
- fclose($fh);
- }
- }
- }
- $zip->close();
- exit();
- }
- }
- else{
- echo "not a zip resource";
- exit();
- }
- }
- }
- else{
- $error = $errors[$_FILES['zip']['error']];
- }
- }
- }
- ?>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <title>Zip to Thumbnails :p</title>
- </head>
- <body>
- <?php if( !empty($error) ){ ?>
- <h3 style="color: red;"><?php echo $error?></h3>
- <?php } ?>
- <form method="post" enctype="multipart/form-data">
- <input type="file" name="zip" />
- <input type="submit" name="submit" value="Envoyer" />
- </form>
- </body>
- </html>
Add Comment
Please, Sign In to add comment