Advertisement
MizunoBrasil

Upload de imagem e após o envio ele mostra o caminho

Jan 23rd, 2023
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. Upload de imagem para pasta images e após o envio ele mostra o caminho (path)
  2.  
  3.  
  4. <?php
  5. if(isset($_FILES['image'])){
  6.     $errors= array();
  7.     $file_name = $_FILES['image']['name'];
  8.     $file_size = $_FILES['image']['size'];
  9.     $file_tmp = $_FILES['image']['tmp_name'];
  10.     $file_type = $_FILES['image']['type'];
  11.     $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
  12.  
  13.     $extensions= array("jpeg","jpg","png");
  14.  
  15.     if(in_array($file_ext,$extensions)=== false){
  16.         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  17.     }
  18.  
  19.     if($file_size > 2097152) {
  20.         $errors[]='File size must be excately 2 MB';
  21.     }
  22.  
  23.     if(empty($errors)==true) {
  24.         move_uploaded_file($file_tmp,"images/".$file_name);
  25.         echo "Success";
  26.     }else{
  27.         print_r($errors);
  28.     }
  29. }
  30. ?>
  31.  
  32. <!DOCTYPE html>
  33. <html>
  34.   <head>
  35.     <title>Image Upload</title>
  36.   </head>
  37.   <body>
  38.     <form action="" method="post" enctype="multipart/form-data">
  39.       <input type="file" name="image" accept="image/*">
  40.       <input type="submit" value="Upload">
  41.     </form>
  42.     <?php if(isset($file_name)) echo "Image path: " . "images/" . $file_name; ?>
  43.   </body>
  44. </html>
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement