dwlakes

accessingfiles

Apr 6th, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title>Uploading...</title>
  5. </head>
  6. <body>
  7.    <h1>Uploading File...</h1>
  8.  
  9. <?php
  10.     if ($_FILES['the_file']['error']){
  11.         echo 'Problem: ';
  12.         switch ($_FILES['the_file']['error']){
  13.             case 1:
  14.                 echo 'File exceeded upload_max_filesize.';
  15.                 break;
  16.             case 2:
  17.                 echo 'File max_file_size.';
  18.                 break;
  19.             case 3:
  20.                 echo 'File only partially uploaded.';
  21.                 break;
  22.             case 4:
  23.                 echo 'No file uploaded.';
  24.                 break;
  25.             case 5:
  26.                 echo 'Cannot upload file: No temp directory specified.';
  27.                 break;
  28.             case 6:
  29.                 echo 'Upload failed: Cannot write to disk.';
  30.                 break;
  31.             case 7:
  32.                 echo 'A PHP extension blocked the file upload.';
  33.                 break;
  34.         }
  35.         exit;
  36.     }
  37.     // Does the file have the right MIME type?
  38.     if ($_FILES['the_file']['type'] != 'image/png'){
  39.         echo 'Problem: file is not a PNG image';
  40.         exit;
  41.     }
  42.  
  43.     // put the file where we'd like it
  44.     $upload_file = '/filesystem/path/to/uploads'.$_FILES['the_file']['name'];
  45.  
  46.     if (is_uploaded_file($_FILES['the_file']['tmp_name'])){
  47.         if (!move_uploaded_file($_FILES['the_file']['tmp_name'], $uploaded_file)){
  48.             echo 'Problem: Could not move file to destination directory.';
  49.             exit;
  50.         }
  51.     }else{
  52.         echo 'Problem: Possible file upload attack. Filename: ';
  53.         echo $_FILES['the_file']['name'];
  54.         exit;
  55.     }
  56.     echo 'File uploaded successfully.';
  57.  
  58.     // show what was uploaded
  59.     echo '<p>You uploaded the following image:<br/>';
  60.     echo '<img src="/uploads/'.$_FILES['the_file']['name'].'"/>';
  61.  
  62. ?>
  63. </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment