The second argument to copy() function cannot be a

2020-02-13 08:16发布

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

标签: php
7条回答
手持菜刀,她持情操
2楼-- · 2020-02-13 08:37

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:/..."

查看更多
▲ chillily
3楼-- · 2020-02-13 08:43

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/'

查看更多
三岁会撩人
4楼-- · 2020-02-13 08:49

Try adding the extension to the name file.

$filename = $_FILES['userfile']['tmp_name'].".jpg";
查看更多
贪生不怕死
5楼-- · 2020-02-13 08:54

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());
        }
    }
查看更多
淡お忘
6楼-- · 2020-02-13 08:55

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 director).

it should be:

move_uploaded_file($_FILES['userfile']['tmp_name'], 'c:/wamp/www/uploads/images/'.$file['name']);
查看更多
We Are One
7楼-- · 2020-02-13 09:00

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']));
查看更多
登录 后发表回答