Move upload file dosen't work? Pls help

I upload the Website and everything work perfect but when i try to upload image dosen’t work.
Image is been saved in database but not in the Uploads folder.

My code :

$user_img = $_FILES[‘register_user_img’][‘name’];
$upload_user_image_path = “/Uploads/users/” . $_FILES[‘register_user_img’][‘name’];

move_uploaded_file($_FILES[‘register_user_img’][‘tmp_name’], $upload_user_image_path);

Try this:-

PHP

<?php
$target_dir = "/Uploads/users/";
$target_file = $target_dir . basename($_FILES["register_user_img"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["register_user_img"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
  if($uploadOk == 1){
     if (move_uploaded_file($_FILES["register_user_img"]["tmp_name"], $target_file)) {
     echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
    }
  }
}

HTML

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="register_user_img" id="register_user_img">
  <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>
3 Likes

Tnx for reply. I try this and result is the same. Image URL is saved in database but Uploads folder is empty. I don’t know where the problem is.

Can you tell me which output is throwing by this code?

File is an image - image/jpeg.Sorry, there was an error uploading your file.

Try:-
Replacing this line to

This

$target_dir = __DIR__."/Uploads/users/";
3 Likes

Working finally.
$upload_user_image_path = “/Uploads/users/” . $_FILES[‘register_user_img’][‘name’];
Solution : Missing dot - “./Uploads/users/” .

3 Likes

Happy Coding :grinning:.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.