Advertisement
Guest User

Baba : Upload Limitation Sample

a guest
Apr 18th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.84 KB | None | 0 0
  1. <?php
  2. error_reporting ( E_ALL );
  3. $allowedExtensions = array (
  4.         'jpg',
  5.         'jpeg',
  6.         'png',
  7.         'bmp',
  8.         'tiff',
  9.         'gif'
  10. );
  11. $maxSize = 2097152;
  12. $dirImage = "photos/tmp_images";
  13. $errors = $output = array ();
  14. if (isset ( $_FILES ['image'] )) {
  15.    
  16.     // Start Validation
  17.     foreach ( $_FILES ['image'] ['tmp_name'] as $key => $val ) {
  18.        
  19.         $fileName = $_FILES ['image'] ['name'] [$key];
  20.         $fileSize = $_FILES ['image'] ['size'] [$key];
  21.         $fileTemp = $_FILES ['image'] ['tmp_name'] [$key];
  22.        
  23.         $fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
  24.         $fileExt = strtolower ( $fileExt );
  25.        
  26.         if (empty ( $fileName ))
  27.         {
  28.             $errors [$fileName] [] = "Missing File Number $key";
  29.         }
  30.        
  31.         if (! in_array ( $fileExt, $allowedExtensions )) {
  32.             $errors [$fileName] [] = "format $fileExt in $fileName is not accepted";
  33.         }
  34.        
  35.         if ($fileSize > $maxSize) {
  36.             $errors [$fileName] [] = "maxsize of 2MB exceeded";
  37.         }
  38.        
  39.         if (! mkdir_recursive ( $dirImage, 0777 )) {
  40.             $errors [$fileName] [] = "Error  Creating /Writing  Directory $dirImage ";
  41.         }
  42.        
  43.         if ($_FILES ['image'] ['error'] [$key] != UPLOAD_ERR_OK) {
  44.             $errors [$fileName] [] = "Upload Errors :  " . $_FILES ['image'] ['error'] [$key];
  45.         }
  46.     }
  47.    
  48.    
  49.     // Only Stat Uploading if no errors
  50.    
  51.     if (empty ( $errors )) {
  52.         foreach ( $_FILES ['image'] ['tmp_name'] as $key => $val ) {
  53.            
  54.             $fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
  55.             $filePrifix = basename ( $fileName, "." . $fileExt );
  56.             $i = 0;
  57.             while ( file_exists ( $fileDst ) ) {
  58.                 $i ++;
  59.                 $fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;
  60.            
  61.             }
  62.             // Move the file
  63.            
  64.             if (count ( $errors ) == 0) {
  65.                 if (move_uploaded_file ( $fileTemp, $fileDst )) {
  66.                     // ...
  67.                    
  68.                     $output [$fileName] = "OK";
  69.                 }
  70.             }
  71.         }
  72.    
  73.     }
  74. }
  75.  
  76. function mkdir_recursive($pathname, $mode) {
  77.     is_dir ( dirname ( $pathname ) ) || mkdir_recursive ( dirname ( $pathname ), $mode );
  78.     return is_dir ( $pathname ) || mkdir ( $pathname, $mode );
  79. }
  80.  
  81.  
  82.  
  83. if (! empty ( $errors )) {
  84.     echo "<pre>";
  85.     foreach ( $errors as $file => $error ) {
  86.         echo $file, PHP_EOL;
  87.         echo "==============", PHP_EOL;
  88.         foreach ( $error as $line ) {
  89.             echo $line, PHP_EOL;
  90.         }
  91.         echo PHP_EOL;
  92.     }
  93.     echo "</pre>";
  94. }
  95.  
  96. if (! empty ( $output )) {
  97.     echo "<pre>";
  98.     echo "Uploaded Files", PHP_EOL;
  99.     foreach ( $output as $file => $status ) {
  100.         echo $file, "=", $status, PHP_EOL;
  101.     }
  102.    
  103.     echo "</pre>";
  104. }
  105. ?>
  106.  
  107.  
  108. <form method="post" enctype="multipart/form-data">
  109.     <label for="file">Filename 1:</label> <input type="file" name="image[]"
  110.         id="file" /> <br /> <label for="file">Filename 2:</label> <input
  111.         type="file" name="image[]" id="file" /> <br /> <label for="file">Filename
  112.         3:</label> <input type="file" name="image[]" id="file" /> <br /> <input
  113.         type="submit" name="submit" value="Submit" />
  114. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement