Jump to content
*Most Powerful Pony!*

How to correctly and easiest download a file from a URL to a folder assets

Recommended Posts

Hey,

Here's a snippet that does the job for me.

$file_source = 'http://www.openbsd.org/images/puffy54.gif';

$file_content = file_get_contents($file_source);

$target_page_id = '9999'; // replace with a real id

$target_page = $pages->get($target_page_id);
$target_page->of(false);

$file_target = $config->paths->files . $target_page_id . '/' . 'puffy54.gif'; // or do basename() on $file_source

file_put_contents($file_target, $file_content);
$file_field_name = 'attachment';

$target_page->$file_field_name->add($file_target);
$target_page->save();
  • Like 5

Share this post


Link to post
Share on other sites

I am sure I am missing something about what you want to do, but this works for me:

$target_page = $pages->get(xxxx);

$target_page->files->add("full url to file"); 

$target_page->of(false);
$target_page->save();
 

I haven't looked to see what PW is using in the background (maybe file_get_content with a fallback to cURL), but there might be an issue with file_get_contents if you have allow_url_fopen off in your server config.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...