Php copy () not working

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

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
source share
3 answers

Read your mistake.

 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 a directory.

Simple debugging can 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 $copyvalue in your example is empty.


If you are wondering why this returns TRUE ...

 if(file_exists($image_to_copy)){ 

.. this is because the ../../images/merchant/particulars/208/ directory exists.

As the manual says:

You must change it to:

 if(is_file($image_to_copy)){ 

Another thing is that the destination file must also be a file:

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

Guide:

+5
source

I think you are confusing a simple thing. Here is a simple example of how to use 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'); 
0
source

Use this function

 !move_uploaded_file($image_to_copy, $dir) 

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

-3
source

All Articles