2016-11-02 89 views
0

下面是從url下載文件的腳本。我想要的是多鏈接,如應該有三個或更多的輸入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); }?> 
+0

你爲什麼不試試[爆炸](http://php.net/manual/en/function.explode.php)?打破它在新行 – Rohit

+0

請你能解釋多一點,因爲我是一個新的即時通訊在PHP捲曲(:: – Abdulmanan

回答

0

歇字符串轉換成使用\ndelimiter數組,你會得到URL的數組。請檢查以下示例以使用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); 
} 
?> 
+0

嗯我在腳本中有一個小問題的文件名是不是隨文件其唯一file.extension和我無法下載多個文件,請幫我在我的腳本中。 – Abdulmanan

+0

所有文件的文件大小都是0kb下載 – Abdulmanan

+0

嘗試使用print_r($ url)來打印數組,請檢查您是否獲得正確的URL? – Rohit