2

I am trying to copy a remote file to a local path using the PHP copy() function.

 // https://d47lev3ki2x1o.cloudfront.net/5804aaa8-e5a8-484a-9c3b-4a72c0a83865-1.jpg
 $imageURL = Configure::read('cloudfront') . $photo['image_location'];
 $localURL =  $this->webroot . 'img/tmp/' . $photo['image_location'];


 if(!@copy($imageURL, $localURL)) {
     $errors= error_get_last();
     echo "COPY ERROR: ".$errors['type'];
     echo "<br />\n".$errors['message'];
 } else {
     echo "File copied from remote!";
 }

It keeps giving me the error:

COPY ERROR: 2 copy(/baker-and-co/img/tmp/5804a6a8-9c78-49f2-88cc-49f5c0a83865-1.jpg): failed to open stream: No such file or directory

The tmp directory exists already on my local so I don't see what the issue is here. I have allow_url_fopen set to true also.

Any ideas?

user3574492
  • 6,225
  • 9
  • 52
  • 105

1 Answers1

0

You may try this to create a new file as you can not coppy a remote file.

 $imageURL = Configure::read('cloudfront') . $photo['image_location'];
 $localURL =  $this->webroot . 'img/tmp/' . $photo['image_location'];
if(!file_put_contents($localURL, file_get_contents($imageURL));) {
 $errors= error_get_last();
 echo "COPY ERROR: ".$errors['type'];
 echo "<br />\n".$errors['message'];
} else {
 echo "File copied from remote!";
}
Pradyut Manna
  • 588
  • 1
  • 3
  • 12
  • You most certainly can copy remote files, see the [docs](http://php.net/manual/en/function.copy.php) , this code gives the same error – user3574492 Nov 18 '16 at 11:28
  • That link is exactly what I have tried, copy() can use a URL for the source file but not for the destination. So again this does not help as I am copying FROM a URL and TO my local file path – user3574492 Nov 18 '16 at 12:15