我想制作一个小脚本,允许将多个文件从一台服务器上传到另一台服务器。问题是当我添加超过 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>
最佳答案
我相信您正在下载的站点正在限制您的脚本的速率。您的脚本每分钟创建的请求可能多于允许的数量,这就是为什么后者的请求为空。
要解决此问题,您可能需要添加另一个 cURL 系列函数,专为您的用例设计:curl_multi_select . 它“阻止 [执行流],直到任何 curl_multi 连接上有事件”(或达到超时)。
你可以尝试这样使用它:
do {
if (curl_multi_select($mh, 0.25) == -1) {
usleep(100);
}
$n = curl_multi_exec($mh, $active);
}
while ($active);
关于php - 多个远程上传服务器到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460513/