需要帮助使这个curl脚本多链接

时间:2016-11-02 17:40:46

标签: php html curl

以下是从url下载文件的脚本。我想要的是多链接,就像应该有三个或更多输入URL框,用户放置他们的链接,脚本下载所有文件。我不想按一个按钮,而是出现另一个网址框;这不是我想要的,我已经尝试过了。或多链接;像这样我们可以在每一行上放置链接:

enter image description here

<?php
class Download {
    const URL_MAX_LENGTH=2000;

    // clean url
    protected function cleanUrl($url){
        if (isset($url)){
            if (!empty($url)){
                if(strlen($url)< self::URL_MAX_LENGTH){
                    return strip_tags($url);
                }
            }
        }
    }
    //is url
    protected function isUrl($url){
        $url=$this->cleanUrl($url);
                if (isset($url)){
                    if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)){
                        return $url;
                    }
                }
    }


    //return extension 
    protected function returnExtension($url){
        if ($this->isUrl($url)){
            $end = end(preg_split("/[.]+/", $url));
            if (isset($end)){
                return $end;
            }
        }
    }

    // file download 
    public function downloadFile($url){
        if ($this->isUrl($url)){
        $extension = $this->returnExtension($url);
        if ($extension){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $return = curl_exec($ch);
        curl_close($ch);


        // directory where files should be downloaded
    $destination = "uploads/file.$extension";
        $file = fopen($destination, "w+");
        fputs($file, $return);
        if (fclose($file)) {
            echo "Successfully Download The File";
        }
        }
        }
    }

}
$obj = new Download();
if (isset($_POST['url'])) { $url = $_POST['url'];}

?>
<form action="index.php" method="post">
<input type="text" name="url" maxlength="2000">
<input type="submit" value="Download" />
</form>
<?php if (isset($url)) { $obj->downloadFile($url); }?>

1 个答案:

答案 0 :(得分:0)

使用\n作为delimiter将字符串分解为数组,您将获得一系列网址。请查看以下示例以使用explode

注意:使用<textarea>,如果您使用input并按enter,则表单将会提交。

<form action="" method="post">
<textarea type="text" name="url" maxlength="2000"></textarea>
<input type="submit" value="Download" />
</form>

<?php
if(isset($_POST['url'])){
    $urls = explode("\n",$_POST['url']);
}
foreach ($urls as $url) {
    echo $url;
    //$obj->downloadFile($url);
}
?>