add file to $_FILES array by reading remote file

I'm pretty new to php and we're trying to write a plugin for wordpress. We have a server with images on it and we'd like to have the plugin have a list of images to download from the server. It then needs to go through that list and read each image from the server into the $_FILES variable that we can then pass to the wordpress media_handle_upload function.

I've been able to read a remote file with the following code. But I'm not sure where to go from here.

$url = 'http://www.planet-source-code.com/vb/2010Redesign/images/LangugeHomePages/PHP.png';

$img = curl_init();
curl_setopt($img, CURLOPT_URL, $url);
curl_setopt($img, CURLOPT_HEADER, 1);
curl_setopt($img, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($img, CURLOPT_BINARYTRANSFER, 1);
$file = curl_exec($img);
curl_close($img);


$file_array = explode("\n\r", $file, 2);
$header_array = explode("\n", $file_array[0]);
foreach($header_array as $header_value) {
  $header_pieces = explode(':', $header_value);
  if(count($header_pieces) == 2) {
    $headers[$header_pieces[0]] = trim($header_pieces[1]);
  }
}
header('Content-type: ' . $headers['Content-Type']);
header('Content-Disposition: ' . $headers['Content-Disposition']);


$imgFile = substr($file_array[1], 1);
echo $imgFile;

Solutions

Solution: Lookup table

Create images name list or image file path(link) list as json,xml or txt format. so it will act like as lookup table. it can be parse easily (just like RSS feed customization). call the json or xml file and get the data in the form of array. now you can process it easily

Similar questions

How can sanitize $_FILES['haq_slider'] field
I have a function I want to SANITIZE this field $sliderfile = $_FILES['haq_slider']; How can i do it
register_rest_field update_callback don't work for $_FILES
I try to insert a post with REST API who contain text and image in one api call. My image is an acf field. I register my field like that: I send my request via Postman update_field_headline is correctly call, but update_field_logo is never call. I try to debug wp-includes/rest-api/endpoints/class-wp-rest-controller.php on line 421 is see: I think u...
Image upload via FormData API and AJAX is not working ($_FILES always empty)
When I try to do what's mentioned in the title, my requests all work (nonce check passes, and server response 200 receveived, and server - script also working properly). My problem is that I cannot access the image file I upload on the server-side script; $_FILES is always empty. Although there are already a lot of posts about this on stack overflo...
$_FILES empty on created_{taxonomy} and create_{taxonomy} yet is is not on edit_{taxonomy}
been struggling with a bug. On created_{taxonomy} hook yet in edited_{taxonomy} hook works and upload the expected file with no issues. As per the following code I added custom field type along with custom taxonomy type. works fine. As per the following code is to add enctype on both edit and add(new)to my custom taxonomy (work_type) works fine it ...
$_FILES is empty when working with wordpress custom field image uploading but, its working in core php website
in wordpress functions.php file; custom field methos are: when i try the above code with core php it works fine but, when i try to do the same in wordpress custom fields image uploading: $_FILES alwasy gives empty. it gives the name of the image if i use $_POST["pic"]; I have tried to check this with print_r, var_dumb and even in wordpress function...
PHP multiple $_FILES for forms
I am a newbie in php and using WordPress as my CMS. I am trying to enable an audio upload with desired featured image in WordPress front end form The PHP code of form is The insert_image and Insert_attachment functions are And And the basic html is Audio Audio featured Image When i submit the form I see only $_FILES['audio'] Works and i cant see th...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .