1

这是我用来将一些图像从一台服务器传输到另一台服务器的基本传输器。这是一个基本脚本,允许我输入 URL,单击下载,它将保存到我服务器上的目录并显示新 URL。

我需要添加一些东西,同时自动给我的图像一个随机数字文件名。我今晚玩了它并且非常接近,但没有任何运气。您可以将功能添加到工具中吗?我很着急,因为我需要在星期一之前使用该工具进行项目,所以我决定寻求帮助来完成这项工作。提前致谢!!!!

<?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) <b>Sucess! Your image has been transloaded from:</b> <br><small><small>$url</small></small><p><b>Your new image URL is:</b><br>http://myURL.com/gallery/$g<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">Download Remote Files And Save Locally</label>
        </legend>
        <form method=POST>
            <label for="url">URL: </label><br />
            <textarea rows=15 cols=75 id="url" name="url"><?= $URL ?></textarea><br />
            <label for="folder">Local folder : </label><input type=text id="folder" name="folder" value="downloads"/>
            <input type=submit name="submit" value="Download Files!" />
        </form>
</fieldset>
4

0 回答 0