0

I was trying to upload a folder inside my htdocs folder in XAMPP.

I followed the rules of move_uploaded_file still did not work.

Here's my current code:

<?php 
   if(isset($_POST['submit'])){
      $allowed_ext = array('png', 'jpg', 'jpeg', 'gif');

      if(!empty($_FILES['upload']['name'])){
        print_r($_FILES);
        $file_name = $_FILES['upload']['name'];
        $file_size = $_FILES['upload']['size'];
        $file_tmp = $_FILES['upload']['tmp_name'];
        $target_dir = "uploads/{$file_name}";

        // Get file ext
        $file_ext = explode('.', $file_name);
        $file_ext = strtolower(end($file_ext));

        // Validate file ext
        if(in_array($file_ext, $allowed_ext)) {
            // verify size
            if($file_size <= 1000000) { // 1000000 bytes = 1MB
              // Upload file
              move_uploaded_file($file_tmp, $target_dir);
              $message = '<p style="color: green;">File uploaded!</p>';
            } else {
                $message = '<p style="color: red;">File to large</p>';
            }
        } else {
          $message = '<p style="color: red;">Invalid file type</p>';
        }
      } else {
        $message = '<p style="color: red;">Please choose a file</p>';
      }
   }
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Upload</title>
</head>
<body>
    <?php echo $message ?? null; ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="upload" />
    <input type="submit" value="submit" name="submit" />
</form>
</body>
</html>

Right now the images are not getting move to the uploads folder inside my current directory.

Any idea why?

Kurt Math
  • 13
  • 1
  • 1
    What is the message you are getting – Shibon Jan 02 '23 at 09:01
  • 1
    Add the html code too – Shibon Jan 02 '23 at 09:01
  • It just says file uploaded was successfully. When i check the uploads folder. I cant find the file I uploaded – Kurt Math Jan 02 '23 at 09:02
  • 1
    Check whether the upload folder has write permission – Shibon Jan 02 '23 at 09:04
  • if(move_uploaded_file($file_tmp, $target_dir)) { $message = '

    File uploaded!

    '; } else { $message = '

    File uploaded failed!

    '; }
    – Shibon Jan 02 '23 at 09:05
  • Now it says `File uploaded failed!` but no specific info why – Kurt Math Jan 02 '23 at 09:12
  • Make sure you show [all errors and warnings](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) while developing locally. If it's on a public server, then you can check PHP's error log for warnings/errors instead. I would also recommend reading [this article](https://phpdelusions.net/articles/paths) (read the whole thing) regarding how to reference files and folders on the server in PHP (since `"upload/"` in the same file can reference different folders, depending on context that file is loaded/used in) – M. Eriksson Jan 02 '23 at 09:17
  • _Side note:_ An easier way to get a file extension is to use [pathinfo()](https://www.php.net/manual/en/function.pathinfo.php): `$ext = pathinfo($filename, PATHINFO_EXTENSION);` – M. Eriksson Jan 02 '23 at 09:20
  • Don't add code into comments - it's difficult to read - edit your original question instead and put it in there. – droopsnoot Jan 02 '23 at 09:39

1 Answers1

0

Try this:

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $extensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$extensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      
      if($file_size > 1000000){
         $errors[]='File size must be excately 1 MB';
      }
      
      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"uploads/".$file_name); //create a folder  'uploads' in your folder
         echo "Success";
      }else{
         print_r($errors);
      }
   }
?>
Anaswara
  • 48
  • 7