php copy()不起作用

时间:2012-09-01 13:23:29

标签: php copy

我想使用ajax调用做一个简单的复制。这是我的代码,但它不起作用。我得到:副本(../../ images / merchant / particulars / 208 /)无法打开流:在scriptname.php第x行的某个/ 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!";
        }

3 个答案:

答案 0 :(得分:5)

请阅读您的错误。

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

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

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

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

它会告诉您,您示例中的$copyvalue为空。


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

if(file_exists($image_to_copy)){

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

正如手册所说:

您应将其更改为:

if(is_file($image_to_copy)){

另一件事是目的地shuld也是文件:

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

手册:

答案 1 :(得分:0)

我认为你混淆了一件简单的事情。以下是有关如何使用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');

答案 2 :(得分:-3)

使用此功能

!move_uploaded_file($image_to_copy, $dir)

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