search
HomeBackend DevelopmentPHP Tutorial 用stream_系列函数实现远程文件本地化,怎么防止卡死

用stream_系列函数实现远程文件本地化,如何防止卡死
还是我前两天写的代码,已经用于采集内容了,今天用来改进采集到的内容中的图片等远程文件的本地化,有时候还是会卡死,高手看看如何能防止卡死。
代码如下,这个代码还是提供两种方式实现本地化进行比较,时事证明这种流方式实现效率是提高了。

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php $dir = str_replace('\\', '/', dirname(__FILE__)) . '/';
$timeStart = microtime(true);
$data = '';
$urls = array('http://www.jxcms.com/upload/2011/0518/0337319304.jpg', 'http://www.jxcms.com/upload/2011/0310/0951568835.jpg', 'http://www.tzksgs.com/upload/banner1.jpg', 'http://www.tzksgs.com/upload/banner2.jpg');
foreach($urls as $url) {
    copy($url, $dir . 'a_' . basename($url));
}
$timeEnd = microtime(true);
echo sprintf("Spend time: %s second(s)\n", $timeEnd - $timeStart), '<br>';
$timeStart = microtime(true);
function getMoreContent($urls) {
    $timeout = 30;
    $rs = array();
    $sockets = array();
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    foreach($urls as $id => $url) {
        $tmp = parse_url($url);
        $host = $tmp['host'];
        $path = isset($tmp['path'])?$tmp['path']:'/';
        empty($tmp['query']) or $path .= '?' . $tmp['query'];
        if (empty($tmp['port'])) {
            $port = $tmp['scheme'] == 'https'?443:80;
        } else $port = $tmp['port'];
        $fp = stream_socket_client("$host:$port", $errno, $errstr, $timeout);
        if ($fp) {
            $rs[$id] = '';
            $sockets[$id] = $fp;
            fwrite($fp, "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: $userAgent\r\nConnection: Close\r\n\r\n");
        }
    }
    // Now, wait for the results to come back in
    while (count($sockets)) {
        $read = $sockets;
        // This is the magic function - explained below
        if (stream_select($read, $write = null, $e = null, $timeout)) {
            // readable sockets either have data for us, or are failed connection attempts
            foreach ($read as $r) {
                $id = array_search($r, $sockets);
                $data = fread($r, 8192);
                if (strlen($data) == 0) {
                    fclose($r);
                    $tmp = explode("\r\n\r\n", $rs[$id], 2);
                    $rs[$id] = strpos($tmp[0], '200')?$tmp[1]:'';
                    unset($sockets[$id]);
                } else $rs[$id] .= $data;
            }
        }
    }
    return $rs;
}
$rs = getMoreContent($urls);
foreach($rs as $k => $v) {
    @file_put_contents($dir . 'b_' . basename($urls[$k]), $v);
}
$timeEnd = microtime(true);
echo sprintf("Spend time: %s second(s)\n", $timeEnd - $timeStart);

?>





------解决方案--------------------
由于php没有计时器,所以你不可能中断一个没有中断接口的函数的执行
所以底层函数都只适合于理想的条件下

我依然建议你使用多道的curl来完成你的工作
PHP code
$urls = array(
 'http://www.jxcms.com/upload/2011/0518/0337319304.jpg',
 'http://www.jxcms.com/upload/2011/0310/0951568835.jpg',
 'http://www.tzksgs.com/upload/banner1.jpg',
 'http://www.tzksgs.com/upload/banner2.jpg'
);
$mh = curl_multi_init();

foreach ($urls as $i => $url) {
       $conn[$i] = curl_init($url);
       curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER,1);
       curl_multi_add_handle($mh, $conn[$i]);
}

do {
  curl_multi_exec($mh, $active);
  //在这个循环中,你有机会中断程序的执行。curl_getinfo提供了连接的各种信息
}while($active);

foreach ($urls as $i => $url) {
  $fn = basename($url);
  file_put_contents($fn, curl_multi_getcontent($conn[$i]));
  curl_close($conn[$i]);
} <div class="clear">
                 
              
              
        
            </div>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)