1

I have a folder called uploads inside my root directory (C:\xampp\htdocs). I am trying to implement the php function move_uploaded_file(), to store my image file inside that folder. This is my code:

$folder="/uploads";
try{
   $moved=move_uploaded_file($_FILES['file']['tmp_name'][0],$folder.'/'.$_FILES['file']['name'][0]);
}
catch(Exception $e) {
   echo $e->getMessage();  //shows nothing
}
if($moved)
    echo "Moved to location successfully";
else
    echo $folder.'/'.$_FILES['file']['name'][0]; // shows /uploads/business-man.png

I have double checked the file_name, the tmp_name. They are correct. Where am I going wrong?

  • What is your error? Can you check $moved from var_dump etc? What is returning? Can you also wrap it with try catch block? – gguney Mar 12 '22 at 13:25
  • `var_dump` shows the output `bool(false)` –  Mar 12 '22 at 13:27
  • what is $_FILES['file']['tmp_name'][0] ? returning – gguney Mar 12 '22 at 13:29
  • `C:\xampp\tmp\php7E63.tmp` –  Mar 12 '22 at 13:31
  • Did you try to use the absolute path for $folder? – Michael Mar 12 '22 at 13:32
  • So when you join them with $folder var it should be uploads/C:\xampp\tmp\php7E63.tmp ? Can you check and var_dump your second parameter for move_uploaded_file? – gguney Mar 12 '22 at 13:33
  • Like `$folder='C:/xampp/htdocs/uploads/'`? –  Mar 12 '22 at 13:34
  • @Abhishek Please see https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display to enable error reporting to show all errors. Also add the output of `var_dump($_FILES);` to your question. – Progman Mar 12 '22 at 14:16
  • There is no error in `$_FILES`. I can easily fetch all the information. –  Mar 12 '22 at 14:19

0 Answers0