Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4020268
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:12:26+00:00 2026-05-20T10:12:26+00:00

I want to save files from an external server into a folder on my

  • 0

I want to save files from an external server into a folder on my server using fopen, fwrite.

First the page from the external site is loaded, and scanned for any image links. Then that list is sent from an to the fwrite function. The files are created, but they aren’t the valid jpg files, viewing them in the browser it seems like their path on my server is written to them.

Here is the code:

//read the file
$data = file_get_contents("http://foo.html");

   //scan content for jpg links
preg_match_all('/src=("[^"]*.jpg)/i', $data, $result); 

//save img function
function save_image($inPath,$outPath)
{
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}

//output each img link from array
foreach ($result[1] as $imgurl) {
    echo "$imgurl<br />\n";
    $imgn = (basename ($imgurl));
    echo "$imgn<br />\n";
    save_image($imgurl, $imgn);
}

The save_image function works if I write out a list:

save_image('http://foo.html', foo1.jpg);
save_image('http://foo.html', foo1.jpg);

I was hoping that I’d be able to just loop the list from the matches in the array.

Thanks for looking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:12:26+00:00Added an answer on May 20, 2026 at 10:12 am

    There are two problems with your script. Firstly the quote mark is being included in the external image URL. To fix this your regex should be:

    /src="([^"]*.jpg)/i
    

    Secondly, the image URLs are probably not absolute (don’t include http:// and the file path). Put this at the start of your foreach to fix that:

    $url = 'http://foo.html';
    # If the image is absolute.
    if(substr($imgurl, 0, 7) == 'http://' || substr($imgurl, 0, 8) == 'https://')
    {
      $url = '';
    }
    # If the image URL starts with /, it goes from the website's root.
    elseif(substr($imgurl, 0, 1) == '/')
    {
      # Repeat until only http:// and the domain remain.
      while(substr_count($url, '/') != 2)
      {
        $url = dirname($url);
      }
    }
    # If only http:// and a domain without a trailing slash.
    elseif(substr_count($imgurl, '/') == 2)
    {
      $url .= '/';
    }
    # If the web page has an extension, find the directory name.
    elseif(strrpos($url, '.') > strrpos($url, '/'))
    {
      $url = dirname($url);
    }
    $imgurl = $url. $imgurl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert all files to dos to unix format from a folder
I download json data from server and I want to save them to a
I want to extract data from HTML table using Python script and save it
I want to save XML files from a PHP script (original XML file and
i just want to ask, how can i save my json file from url
i want to extract audio from video file and save as a audio file
I want to save files which will have name with danish characters, for example
I want to save a remote img-file to my server, but I don't know
I want to save a pdf and mp3 file(s) to a SQL Server database
I have a image from files = request.FILES['new_photo'].read() and i want to know it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.