php copy() 函数的第二个参数不能是目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2618418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:08:00  来源:igfitidea点击:

The second argument to copy() function cannot be a directory

php

提问by Jorm

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
警告:move_uploaded_file() [function.move-uploaded-file]:copy() 函数的第二个参数不能是第 41 行 C:\wamp\www\upload.php 中的目录
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
警告:move_uploaded_file() [function.move-uploaded-file]:无法将 'C:\wamp\tmp\php1AB.tmp' 移动到 C:\wamp\ 中的 'c:\wamp\www\uploads\images/' www\upload.php 第 41 行

回答by Samuel

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']);

回答by Mark Rushakoff

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

您指定将文件移动到目录;PHPmove_uploaded_file及其copy副本都不像 shell 的副本那样智能——您必须为目标指定文件名,而不是目录。

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

因此,一种简单的解决方案是获取basename源文件的 并将其附加到目标目录。

回答by sblom

It sounds like the second argument to move_uploaded_fileshould 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/'

听起来像 to 的第二个参数move_uploaded_file应该是完整的文件名,而不仅仅是目录名。此外,可能只是一个样式问题,但您应该使用一致的斜线'c:\wamp\www\uploads\images/'

回答by Tyler McHenry

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

因为 PHP 不是 shell。您正在尝试将文件复制到c:\wamp\www\uploads\images目录中,但 PHP 不知道您的意思是当您执行时(在move_uploaded_file函数内):

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.

此命令告诉它将文件重命名为c:\wamp\www\uploads\images/,但它不能这样做,因为这是现有目录的名称。

Instead, do this:

相反,请执行以下操作:

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

回答by alvaroms

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

你应该写完整的路径“C:/...”

回答by fabiodipa

Try adding the extension to the name file.

尝试将扩展名添加到名称文件。

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

回答by Mamun Hasan

It will be like below in phalcon 3.42

它会在 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());
        }
    }