多个远程上传服务器到服务器

时间:2016-11-07 08:11:52

标签: php html css nginx nginx-location

我想制作一个小脚本,允许将多个文件从一个服务器上传到另一个服务器。问题是,当我添加超过100个URL时,它会破坏文件。

任何方式都可以更清洁,因此文件不应该被破坏。

这是脚本:

// 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>

1 个答案:

答案 0 :(得分:0)

我相信您下载的网站是对您的脚本进行速率限制的。您的脚本可能每分钟创建的请求数超过允许的数量,这就是后者请求变空的原因。

要解决此问题,您可能需要添加另一个专为您的用例设计的cURL系列函数:curl_multi_select。 它会阻止[执行流程],直到任何curl_multi连接上有活动为止#34; (或达到超时)。

您可以尝试使用它:

do {
    if (curl_multi_select($mh, 0.25) == -1) {
        usleep(100);
    }

    $n = curl_multi_exec($mh, $active);
}
while ($active);
相关问题