5

我想使用 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!";
        }
4

3 回答 3

6

请阅读您的错误。

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)){

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

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

手动的:

于 2012-09-01T13:27:53.483 回答
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');
于 2013-11-05T08:51:37.357 回答
-3

使用此功能

!move_uploaded_file($image_to_copy, $dir)

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

于 2012-09-01T13:27:30.553 回答