20

Anyone know why this:

<?PHP
$title = trim($_POST['title']);
$description = trim($_POST['description']);

// Array of allowed image file formats 
$allowedExtensions = array('jpeg', 'jpg', 'jfif', 'png', 'gif', 'bmp');

foreach ($_FILES as $file) {
  if ($file['tmp_name'] > '') {
    if (!in_array(end(explode(".",
            strtolower($file['name']))),
            $allowedExtensions)) {
      echo '<div class="error">Invalid file type.</div>';
    }
  }
}

if (strlen($title) < 3)
  echo '<div class="error">Too short title</div>';
else if (strlen($description) > 70)
  echo '<div class="error">Too long desccription.</div>';

else {
  move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');
}

Gives:

Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\wamp\www\upload.php on line 41
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php1AB.tmp' to 'c:\wamp\www\uploads\images/' in C:\wamp\www\upload.php on line 41
3
  • 10
    Yes. It cannot be a directory Commented Apr 11, 2010 at 19:43
  • 1
    Actually this warning is explained in the php manual - php.net/manual/en/function.move-uploaded-file.php.
    – Hanseh
    Commented Apr 11, 2010 at 19:52
  • I was getting this error when the file name was blank - so a bit of misdirection. I did a check to ensure that the file did exist and was named correctly and all was good. Commented Jan 11, 2021 at 23:50

8 Answers 8

41

It's because you're moving a file and it thinks you're trying to rename that file to the second parameter (in this case a directory).

it should be:

move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:/wamp/www/uploads/images/'.$file['name']);
2
  • 2
    use forward slashes instead of backslashes
    – Samuel
    Commented Apr 12, 2010 at 20:39
  • The example is here w3schools.com/php/php_file_upload.asp Even with that, I wasn't paying attention to the fact that the second parameter should have been a file, not a dir. 3 points for googledorf Commented Dec 5, 2012 at 13:52
12

You are specifying to move a file to a directory; neither PHP's move_uploaded_file nor its copy is as smart as a shell's copy -- you have to specify a filename, not a directory, for the destination.

So, one simple solution would be to take the basename of the source file and append that to the destination directory.

1

It sounds like the second argument to move_uploaded_file should be a full file name instead of just the directory name. Also, probably only a style issue, but you should use consistent slashes in 'c:\wamp\www\uploads\images/'

1

Because PHP is not a shell. You're trying to copy the file into the c:\wamp\www\uploads\images directory, but PHP doesn't know you mean that when you execute (within the move_uploaded_file function):

copy($_FILES['userfile']['tmp_name'], 'c:\wamp\www\uploads\images/');

This command tells it to rename the file to c:\wamp\www\uploads\images/, which it can't do because that's the name of an existing directory.

Instead, do this:

  move_uploaded_file($_FILES['userfile']['tmp_name'], 
    'c:\wamp\www\uploads\images/' . basename($_FILES['userfile']['tmp_name']));
1

if you want just copy the file in two Dir different, try this :

if (move_uploaded_file($_FILES['photo']['tmp_name'], $target.$pic1))
{
  copy("C:/Program Files (x86)/EasyPHP-5.3.9/www/.../images/".$pic1, "C:/Program Files (x86)/EasyPHP-5.3.9/www/.../images/thumbs/".$pic1)
}

You should write the complete path "C:/..."

1

Try adding the extension to the name file.

$filename = $_FILES['userfile']['tmp_name'].".jpg";
1

FIRSTLY FIND YOUR CURRENT DIRECTORY echo getcwd(); IF YOU FIND THE CURRENT DIRECTORY SO MOVE THEM ANOTHER DIRECTORY FOR EXAMPLE = echo getcwd(); these output is "your live directory name" like that -> /opt/lampp/htdocs/product and create new directory anywhere but your current directory help to go another directory ***

plz read carefully very thinking answer


1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 6, 2022 at 13:07
0

It will be like below in phalcon 3.42

if ($this->request->hasFiles() == true) {
        // Print the real file names and sizes
        foreach ($this->request->getUploadedFiles() as $file) {

            //Print file details
            echo $file->getName(), " ", $file->getSize(), "\n";

            //Move the file into the application
            $file->moveTo($this->config->application->uploadsDir.$file->getName());
        }
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.