How to deal with image leakage when CURL collects remote images locally

Source: Internet
Author: User
When CURL collects remote images to a local device, it takes some time for the image to be missed and stored locally. in this case, hundreds of images will be collected at once, as a result, many images have not been saved to the local device, and the next one is collected.

If the collection interval is not set, can PHP be used to solve this problem? only after the image is saved locally and executed, the next


Reply to discussion (solution)

Are you collecting multiple threads?
It is best to paste the relevant code

If you only solve the problem of leakage, you can get it several times in a loop. sometimes the opposite server is unstable.

function curl_get($url) {$filename = time ();$curl = curl_init ();curl_setopt ( $curl, CURLOPT_URL, $url );curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );$data = curl_exec ( $curl );curl_close ( $curl );$file_extension = image_type ( $url );$filename .= '.' . $file_extension;$fp = fopen ( './zzz/' . $filename, "w" );fwrite ( $fp, $data );fclose ( $fp );}$content = file_get_contents ( 'img.txt' );$arr = explode ( "\r\n", $content );foreach ( $arr as $v ) {curl_get ( $v );}

Img.txt stores all the remote image addresses. if sleep (2) is used during the loop, all images can be correctly saved to the local device. If no interval is set, several images will be missed. Is there any way to solve this problem?

Don't you use curl_multi? Resources cannot be fully utilized by a single curl
The CURLOPT_FILE parameter can be directly written to the file without manual intervention.

It is not a one-time collection of multiple images, and then published to an article. Instead, the remote images in each article are saved locally, so the single-threaded curl is used.

Collect 100 articles with images to your own program, so that part of the image is missing during the image localization process.

Don't you use curl_multi? Resources cannot be fully utilized by a single curl
The CURLOPT_FILE parameter can be directly written to the file without manual intervention.



CURLOPT_FILE is indeed much faster, but few files are missing.

Is there only one image for an article?
The missing image may not be analyzed.

Is there only one image for an article?
The missing image may not be analyzed.

As long as each cycle interval is 2 seconds, it will be completely saved to the local device. is it impossible to execute the next loop after it is fully executed?

Is there only one image for an article?
The missing image may not be analyzed.

After saving the image as an image, I also executed file_put_contents to write the image address to the text file. there is no small number of image addresses, which indicates that the image resolution is correct, the problem may be that the write takes some time, but the loop does not wait until the write is completed.

I used fwrite ($ fp, file_get_contents ($ url); to solve the problem. But why not curl? Doesn't he wait for the data to return and continue? how can I set curl to get all the data correctly?

Your curl_get cannot be reimported.
The next invocation can be performed only after the previous invocation is completed.
It doesn't matter if you pause in a loop.

Where is the problem? why is the file lost with curl? How should I set it?

CURLOPT_TIMEOUT sets the maximum number of seconds that cURL can be executed

CURLOPT_CONNECTTIMEOUT is the waiting time before the connection is initiated. if it is set to 0, it will wait infinitely.

These two tests should be able to solve the problem. If not, check other parts of the code. this is not necessarily a curl issue.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.