php copy() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12228121/
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 03:05:14  来源:igfitidea点击:

php copy() does not work

phpcopy

提问by jessiPP

I want to do a simple copy using a ajax call. this is my code but it does not work . i kepp getting : a copy(../../images/merchant/particulars/208/) failed to open stream: Is a directory in some/filepath on scriptname.php line x , kind of error.

我想使用 ajax 调用做一个简单的复制。这是我的代码,但它不起作用。我得到:副本(../../images/merchant/particulars/208/)无法打开流:是 scriptname.php 行 x 上 some/filepath 中的目录,有点错误。

corrected code: 

$dir_to_make = '../../images/merchant/particulars/'.$copytothisstore;
$dir = '../../images/merchant/particulars/'.$copytothisstore.'  /'.$copyvalue;
$image_to_copy = '../../images/merchant/particulars/'.$copyfromthisstore.'/'.$copyvalue;






    if(is_file($image_to_copy)){

        //chk if there is a folder created for this store
        if(!is_dir($dir_to_make)){
              mkdir($dir_to_make, 0755);  
              chmod($dir_to_make, 0755);

              //copy the image 
              if (!copy($image_to_copy,$dir)) {
                  echo "failed to copy $image_to_copy\n";
              } else {
                  echo"all is well!!";
              }

           } else {
               chmod($dir_to_make, 0755);
               if (!copy($image_to_copy,$dir)) {
                  echo "failed to copy $image_to_copy\n";
              } else {
                  echo"all is well!!";
              }

           }


           echo"$image_to_copy does exist!";
        } else{
            echo"$image_to_copy does not exist!";
        }

采纳答案by Peter

Please read your error.

请阅读您的错误。

copy(../../images/merchant/particulars/208/) failed to open stream: Is a directory in some/filepath

It says that your source file is not a file, but directory.

它说您的源文件不是文件,而是目录。

Simple debugging could always solve your problems:

简单的调试总能解决你的问题:

$image_to_copy ='../../images/merchant/particulars/'.$copyfromthisstore.'/'.$copyvalue;
echo $image_to_copy; // yes, that could give you the answer

It will show you, that $copyvaluein you example is empty.

它会告诉你,$copyvalue你的例子是空的。



If you wonder why this is returning TRUE...

如果你想知道为什么这会回来TRUE......

if(file_exists($image_to_copy)){

..it's because directory ../../images/merchant/particulars/208/does exists.

..这是因为目录../../images/merchant/particulars/208/确实存在。

As manual says:

正如手册所说:

You should change it to:

您应该将其更改为:

if(is_file($image_to_copy)){


Another thing is destination shuld be file as well:

另一件事是目的地也应该是文件:

copy($image_to_copy, $dir.'file.jpg');

Manual:

手动的:

回答by Ari

I think you are confusing a simple thing. Here is a simple example on how to use copy()

我认为你在混淆一件简单的事情。这是一个关于如何使用的简单示例copy()

$file = 'path/to/filename.ext'; // Example: http://adomain.com/content/image.jpg

$destination_folder = 'path/of/the/destination/folder';
//example: $_SERVER["DOCUMENT_ROOT"]."/content/files/"


// Execute the copy function
copy($file, $destination_folder.'/newFilaname.ext');

回答by user1610684

Use this function

使用这个功能

!move_uploaded_file($image_to_copy, $dir)

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

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