0

Im trying to upload an image after i sent a formular in uploads directory and create a subdirectory for every user.After i sent the formular the image i upload isnt move to the folder.

if (isset($_FILES['filename'])) {

   if (isset($_FILES["filename"]["tmp_name"]) && $_FILES["filename"]["tmp_name"]) {
      $imagename = $_FILES["filename"]["name"];
      $check = mime_content_type($_FILES["filename"]["tmp_name"]);
      // $imgData = addslashes(file_get_contents($_FILES['filename']['tmp_name']));
      // $imageProperties = getimageSize($_FILES['filename']['tmp_name']);
      if ($check != 'image/png' && $check != 'image/jpg') {
         echo "<script>alert('Format fisier gresit');
         window.location.href='programare.php';
         </script>";
      }



      $target_dir = "uploads/" . trim($id, "\"");
      if (!file_exists($target_dir)) {
         mkdir($target_dir, 0777, true);
      }


      if (move_uploaded_file(basename($_FILES["filename"]["tmp_name"]), $target_dir)) {
         echo "The file has been uploaded.";
      } else {
         echo "Sorry, there was an error uploading your file.";
         var_export($_FILES["filename"]);
      }
   } else {
      echo "<script>alert('Introduceti o poza cu problema');
      window.location.href='programare.php';
      </script>";
   }
}

This is the form code

<label>Imagine: </label><input type="file" id="myFile" name="filename" id="filename">

The subfolder is created but the image isnt uploading subfolder this is shown

UPDATE:

   $target_dir = "uploads/";
      $target_file = $target_dir . basename($_FILES["filename"]["name"]);
      if (!file_exists($target_dir)) {
         mkdir($target_dir, 0777, true);
      }


      if (move_uploaded_file(($_FILES["filename"]["tmp_name"]), $target_file)) {
         echo "The file has been uploaded.";
      } else {
         echo "Sorry, there was an error uploading your file.";
         var_export($_FILES["filename"]);
      }

this works, it save all the images in the uploads folder, but the logic above, doesnt move the files to new subfolder with the name of userid instead it replace the name of file , if the i upload image.png , it will upload 1image.png for user with id 1.

3
  • Have you verified permissions are correctly set on $target_dir so that the server is able to write to it? Commented May 29, 2022 at 10:27
  • yes it s not a problem Commented May 29, 2022 at 10:38
  • Probably something with $target_file = $target_dir . basename($_FILES["filename"]["name"]); not set to the correct place, how about try print_r($target_file); before the move_uploaded_file() to see where its going? Commented May 29, 2022 at 10:50

1 Answer 1

1

There is a few issues with your code.

For the move_uploaded_file() function, you need to pass the source and destination path relative to the current directory. However you have passed only the filename as first argument and you haven't specified the total destination file path for the second argument.

You have used basename() which returns the filename in the path.

Also your $target_dir variable doesn't contain the filename along with the directory.

Example for a correct path for 2nd argument: uploads/1/filename.png

Example of what you have passed as 2nd argument: uploads/1/

The warning:

Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory

The correct code would be (for the move_uploaded_file()):

<?php

if (move_uploaded_file($_FILES["filename"]["tmp_name"], $target_dir . $_FILES["filename"]["name"])) {
         echo "The file has been uploaded.";
      } else {
         echo "Sorry, there was an error uploading your file.";
         var_export($_FILES["filename"]);
      }

?>

Output:

The file has been uploaded.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.