2010-10-26 88 views
0

朋友的文件夾操作,問題與PHP

我有一個問題............

請幫助我........

我從我的客戶端獲取圖像URL,我想將這些圖像存儲在本地文件夾中。

如果這些圖像是少,我會救他們手動

但他們比5000倍更大的圖像.........

請給一些代碼來降負荷的所有圖片與PHP

+0

所以URL只是目錄列出了一個文件夾的URL,你想從中獲取所有的圖像文件?我正確地認爲? – Malachi 2010-10-26 14:07:54

+0

您的客戶是否爲您提供每張圖片的網址列表,或者它是一張包含所有圖片或少量HTML頁面的HTML頁面? – 2010-10-26 14:10:32

回答

0

你可以嘗試file_get_contents爲此。只是遍歷文件數組並使用file_get_contents('url');將文件檢索到一個字符串,然後file_put_contents('新文件名');再次寫入文件。

0

您可以使用PHP函數file_get_contents()下載文件,然後將其寫入本地計算機,例如使用fwrite()函數。

唯一打開的問題是,在哪裏得到應該下載的文件列表 - 你沒有在你的問題中指定它。

法草案:

$filesList = // obtain URLs list somehow 
$targetDir = // specify target dir 
foreach ($filesList: $fileUrl) { 
    $urlParts = explode("/", $fileUrl); 
    $name = $urlParts[count($urlParts - 1)]; 
    $contents = file_get_contents($fileUrl); 

    $handle = fopen($targetDir.$filename, 'a'); 
    fwrite($handle, $contents); 
    fclose($handle); 
} 
0

我不知道這是你想要的。給定一個文件夾的(如PHP有權力獲得該文件夾的內容)的網址,你想寫一個網址,這將複製所有文件:

function copyFilesLocally($source, $target_folder, $index = 5000) 
{ 
    copyFiles(glob($source), $target_folder, $index); 
} 

function copyFiles(array $files, $target_folder, $index) 
{ 
    if(count($files) > $index) 
    { 
     foreach($files as $file) 
     { 
      copy($file, $target_folder . filename($file)); 
     } 
    } 
} 

如果你正在尋找一個遠程服務器,試試這個:

function copyRemoteFiles($directory, $target_folder, $exclutionFunction, $index = 5000) 
{ 
    $dom = new DOMDocument(); 
    $dom->loadHTML(file_get_contents($directory)); 
    // This is a list of all links which is what is served up by Apache 
    // when listing a directory without an index. 
    $list = $dom->getElementsByTagName("a"); 
    $images = array(); 
    foreach($list as $item) 
    { 
     $curr = $item->attributes->getNamedItem("href")->nodeValue; 
     if($exclutionFunction($curr)) 
      $images[] = "$directory/$curr"; 
    } 
    copyFiles($images, $target_folder, $index); 
} 

function exclude_non_dots($curr) 
{ 
    return strpos($curr, ".") != FALSE; 
} 

copyRemoteFiles("http://example.com", "/var/www/images", "exclude_non_dots");