2011-03-29 59 views
0

外部文件我有一個網址,一個數組,如:節省PHP

[1] = http://site.com/1.pdf 
[2] = http://site.com/234234234.png 
[3] = http://site.com/archive.zip 
[4] = http://site.com/1f41f.anyformat 
[5] = http://site.com/file.txt 

如何將它們保存到某個文件夾在我的FTP的PHP?

文件的名稱不應更改。

+0

爲了讓他們使用大概捲曲,將它們存儲在FTP上使用FTP擴展,這就是它的作用:)文檔中有很多例子。 – Wrikken 2011-03-29 00:22:56

回答

1

也許這將幫助你解決問題

function remote_merge($sourceurl,$targetftp){ 
    $ch = curl_init ($sourceurl); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
    $rawdata=curl_exec($ch); 
    curl_close ($ch); 
    $tempfile = "/path/to/temp/".basename(parse_url($sourceurl, PHP_URL_PATH)); 
    if(file_exists($tempfile)){ 
     unlink($tempfile); 
    } 
    $fp = fopen($tempfile,'x'); 
    fwrite($fp, $rawdata); 
    fclose($fp); 

    $ch = curl_init(); 
    $fp = fopen($tempfile, "rb"); 

    curl_setopt($ch, CURLOPT_URL, $targetftp); 
    curl_setopt($ch, CURLOPT_UPLOAD, 1); 
    curl_setopt($ch, CURLOPT_INFILE, $fp); 
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($tempfile)); 
    $error = curl_exec($ch); 
    // check $error here to see if it did fine or not! 
    curl_close($ch); 
} 

使用此試訓的remote_merge功能

$sourceurls = array(
    "http://site.com/1.pdf", 
    "http://site.com/234234234.png", 
    "http://site.com/archive.zip", 
    "http://site.com/1f41f.anyformat", 
    "http://site.com/file.txt" 
); 

foreach($sourceurl as $sourceurls){ 
    $filename = basename(parse_url($sourceurl, PHP_URL_PATH); 
    $targetftp = "ftp://${ftpuser}:${ftppasswd}@${ftpserver}${ftppath}/$filename"; 
    remote_merge($sourceurl,$targetftp) 
} 
0

193個問題,3個回答...哇。

function curl($url){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_close ($ch); 
    return curl_exec($ch); 
} 

$files = array(); 
$dir = "dir/"; 

foreach($files as $file){ 
    $name = explode("/", $file); 
    $name = end($name); 
    $contents = curl($file); 
    file_put_contents($dir.$name, $contents); 
} 
+0

'$ name = basename(parse_url($ file,PHP_URL_PATH));',否則就是一個很好的答案。這樣它將正確處理查詢字符串和錨點。 – 2011-03-29 00:32:29

+0

我是由示例數組領導的,但您的建議很好。謝謝。 – 2011-03-29 00:34:47

1

這裏有一個簡單的例子:

$urls = array('url1', 'url2'); 
foreach($urls as $url) { 
    $data = file_get_contents($url); 
    file_put_contents('/path/to/folder/'.basename($url), $data); 
}