加入两个脚本一起工作

时间:2016-11-14 10:12:14

标签: php html curl file-upload upload

如上所述,它为加入两个脚本。我试图在服务器之间转换文件,并成功完成了Curl PHP函数。

问题是我有大量的URL要传输,它一次发送多个并行请求,服务器阻止它。

1.这是转载文件的脚本

<?php
// Check if form has been submitted
if(@$_POST['submit']){
    ini_set("max_execution_time", 0);  // no time-outs!
    ignore_user_abort(true);   // Continue downloading even after user closes the browser.
    // URLS -- One on each line
    $URL = $_POST['url'];
    // Relative path to Save downloaded images
    // Default is "downloads"
    // Make sure that it is writable (chmoded correctly)
    $folder = $_POST['folder']; 
    // Check if User has provided a local folder
    if (!$folder || !isset($folder)){
        // Generate error if left blank by user.
        die ("Please specify local folder name");
    }
    // Split all URLS into an array
    $urls = split("\n", $URL);
    // Remove Carriage Returns (useful for Windows-based browsers)
    $urls = str_replace("\r", "", $urls);
    $mh = curl_multi_init();
    foreach ($urls as $i => $url) {
        $path = pathinfo($url);
        $g=$folder . "/" . $path["basename"] ;
        // Check if file already exists on local folder.
        if(file_exists($g)){
            // If exists, delete the file so it always contains the latest update. 
            unlink($g) or die("Unable to delete existing '$g'!");
        }
        // Update the user of what's going on
        echo "$i) Downloading: from <b>$url</b> to <a href=\"$g\"><b>$g</b></a><br />";
        if(!is_file($g)){
            $conn[$i]=curl_init($url);
            $fp[$i]=fopen ($g, "w");
            curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
            curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
            // curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,1000);
            curl_multi_add_handle ($mh,$conn[$i]);
        }
    }
    do {
        $n=curl_multi_exec($mh,$active);
    } while ($active);
    foreach ($urls as $i => $url) {
        curl_multi_remove_handle($mh,$conn[$i]);
        curl_close($conn[$i]);
        fclose ($fp[$i]);
    }
    curl_multi_close($mh);
} // task closed
?>
<br />
<br />
<fieldset>
<legend>
<label for="url">Server to Server Upload Script</label>
</legend>
<form method=POST>
<label for="url">Insert Files URL, One Per Line: </label><br />
<textarea rows=15 cols=75 id="url" name="url"><?= $URL ?></textarea><br />
<label for="folder">Folder Name: </label><input type=text id="folder" name="folder" value="uploads"/>
<input type=submit name="submit" value="Start Uploading Files!" />
</form>
</fieldset>

2以下是一次处理多个请求的代码,此脚本也写得很好,有关以下代码的更多信息可以建立在onlineaspect

function rolling_curl($urls, $callback, $custom_options = null) {

    // make sure the rolling window isn't greater than the # of urls
    $rolling_window = 5;
    $rolling_window = (sizeof($urls) &lt; $rolling_window) ? sizeof($urls) : $rolling_window;

    $master = curl_multi_init();
    $curl_arr = array();

    // add additional curl options here
    $std_options = array(CURLOPT_RETURNTRANSFER =&gt; true,
    CURLOPT_FOLLOWLOCATION =&gt; true,
    CURLOPT_MAXREDIRS =&gt; 5);
    $options = ($custom_options) ? ($std_options + $custom_options) : $std_options;

    // start the first batch of requests
    for ($i = 0; $i &lt; $rolling_window; $i++) {
        $ch = curl_init();
        $options[CURLOPT_URL] = $urls[$i];
        curl_setopt_array($ch,$options);
        curl_multi_add_handle($master, $ch);
    }

    do {
        while(($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
        if($execrun != CURLM_OK)
            break;
        // a request was just completed -- find out which one
        while($done = curl_multi_info_read($master)) {
            $info = curl_getinfo($done['handle']);
            if ($info['http_code'] == 200)  {
                $output = curl_multi_getcontent($done['handle']);

                // request successful.  process output using the callback function.
                $callback($output);

                // start a new request (it's important to do this before removing the old one)
                $ch = curl_init();
                $options[CURLOPT_URL] = $urls[$i++];  // increment i
                curl_setopt_array($ch,$options);
                curl_multi_add_handle($master, $ch);

                // remove the curl handle that just completed
                curl_multi_remove_handle($master, $done['handle']);
            } else {
                // request failed.  add error handling.
            }
        }
    } while ($running);

    curl_multi_close($master);
    return true;
}

任何帮助一起工作?我尝试但没有工作,也不是PHP的专家。

0 个答案:

没有答案