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?

2
  • what is your current folder ? Commented Nov 18, 2016 at 10:52
  • I'm using CakePHP MVC structure so I am in /View/Reports/admin_view.ctp and the folder is located in /webroot/img/tmp Commented Nov 18, 2016 at 11:30

1 Answer 1

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!";
}
Sign up to request clarification or add additional context in comments.

2 Comments

You most certainly can copy remote files, see the docs , this code gives the same error
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.