1

I am trying to move one image to new folder move_uploaded_file is returning 1 but the file is missing, I am working on localhost with XAMPP

$name = basename($_FILES['arr']['name'][0]);
move_uploaded_file($_FILES['arr']['tmp_name'][0],"\Images");
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
  • 1
    Possible duplicate of [PHP - using move\_uploaded\_file](https://stackoverflow.com/questions/32510156/php-using-move-uploaded-file) – yivi Oct 25 '17 at 14:45
  • I would use single quotes around \Images instead of double quotes. – Brogan Oct 25 '17 at 14:46

2 Answers2

0
$name = basename($_FILES['arr']['name'][0]);
move_uploaded_file($_FILES['arr']['tmp_name'][0],'/images/' . $filename);

You have to add a destination for the file to go, including the filename that you wish to assign to it.

Refer to: http://php.net/manual/en/function.move-uploaded-file.php

  • 1
    [`"\n"`](http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double) means new line. I doubt a file name that contains it is useful and usable. – axiac Oct 25 '17 at 14:44
  • Well if you read the PHP documentation it states, ' If the file is valid, it will be moved to the filename given by destination.'. Within the code he has written there is only a directory path and not a filename. –  Oct 25 '17 at 14:46
  • What @axiac means is that inside double quotes, a `\n` will be interpreted as a newline, so your filename is probably borked. Maybe you meant `"\\Images\\newname"`...? No idea. – yivi Oct 25 '17 at 14:48
  • @JosephHarris is correct ! he missed the filename, and his and my answer are correct. There is no point to downvote. – Thamaraiselvam Oct 25 '17 at 14:48
  • Actually, axiac is right. You should use single quotes instead of double quotes to prevent \n being translated to a new line. – Brogan Oct 25 '17 at 14:48
  • On Linux and macOS, the forward slash (`/`) is the directory separator but it is perfectly safe to use it on Windows too. PHP handles both `/` and `\ ` as directory separators on Windows. – axiac Oct 25 '17 at 15:04
  • Yeah, so its okay to use forward slash instead of confusing both. – Thamaraiselvam Oct 26 '17 at 05:09
-1

You should specify the destination file name

if ( move_uploaded_file($_FILES['arr']['tmp_name'],dirname(__FILE__) . '/images/' . $_FILES['arr']['name'] ) ) {
    echo "file uploaded";
} else {
   echo "error in uploading";
}
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71