Carp Valentin
Carp Valentin

Reputation: 21

PHP uploading images

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.

Upvotes: 0

Views: 104

Answers (1)

mani-hash
mani-hash

Reputation: 784

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

Upvotes: 1

Related Questions