wordpress gallery images

The following code is in php use to upload images into the wordpress gallery.
if( !empty($_FILES['gallery_image'])){

 add_post_meta($property_id, 'REAL_HOMES_property_images', $uploaded_file_id);*/

 $files = $_FILES['gallery_image'];
 foreach ($files['name'] as $key => $value) {
  if ($files['name'][$key]) {
   $file = array(
     'name' => $files['name'][$key],
     'type' => $files['type'][$key],
     'tmp_name' => $files['tmp_name'][$key],
     'error' => $files['error'][$key],
     'size' => $files['size'][$key]

   $_FILES = array("gallery_image" => $file);

   foreach ($_FILES as $file => $array) {

    $uploaded_file_id = insert_attachment( $file, $property_id );

    add_post_meta($property_id, 'REAL_HOMES_property_images', $uploaded_file_id);

Open in new window

But instead of uploading images from my computer, I have to enter the image url. The code needs to get the image from the url and save it as same above. How can I alter the above code to do that??
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
"File upload" is a term of art in web development.  It means copying a file from your client computer to the server via the HTTP post method.  What you probably want to do is use PHP file_get_contents() to read images from another server.  It's a completely different process from file uploads.  I don't know WP well enough to advise you exactly how to do this in the "WordPress way" but I can describe the general design.

1. Your server puts up an HTML document with a form containing a text input control
2. The client pastes a URL in there
3. On submit, your server reads the file from the URL that is posted, and writes the results (if any) into a directory on your server, using the "basename" of the URL as the local file name.
4. Then you can create a URL to the file on your server and use the URL in <img> tags, etc.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.