2013-05-19 35 views
0

我正在使用PHP文件抓取器腳本。我將遠程文件的URL放在字段​​上,然後將文件直接上傳到我的服務器。代碼如下所示:獲取遠程文件抓取後的文件名

<?php 
ini_set("memory_limit","2000M"); 
ini_set('max_execution_time',"2500"); 

foreach ($_POST['store'] as $value){ 
    if ($value!=""){ 
     echo("Attempting: ".$value."<br />"); 
     system("cd files && wget ".$value); 
     echo("<b>Success: ".$value."</b><br />"); 
    } 
} 

echo("Finished all file uploading."); 
?> 

上傳文件我想顯示直接鏈接到文件後:例如

完成了所有文件上傳,直接網址: http://site.com/files/grabbedfile.zip

你能幫我確定這段代碼中最後上傳文件的文件名嗎?

在此先感謝

+3

您的腳本通過'$ _ POST [「商店」]'是脆弱的執行任意命令。 – Gumbo

+0

我不知道從sourceforge下載了它:( –

+2

查看'system()'做了什麼,然後考慮當$ _POST ['store']'包含'http://example.com && rm -rf時會發生什麼/' – Gordon

回答

0

您可以使用wget日誌文件。只需添加-o logfilename
這裏是一個小function get_filename($wget_logfile)

ini_set("memory_limit","2000M"); 
ini_set('max_execution_time',"2500"); 

function get_filename($wget_logfile) 
{ 
    $log = explode("\n", file_get_contents($wget_logfile)); 
    foreach ($log as $line) 
    { 
     preg_match ("/^.*Saving to: .{1}(.*).{1}/", $line, $find); 
     if (count($find)) 
      return $find[1]; 
    } 
    return ""; 
} 

$tmplog = tempnam("/tmp", "wgetlog"); 
$filename = ""; 

foreach ($_POST['store'] as $value){ 
    if ($value!=""){ 
     echo("Attempting: ".$value."<br />"); 
     system("cd files && wget -o $tmplog ".$value); // -o logfile 

     $filename = get_filename($tmplog); // current filename 
     unlink ($tmplog); // remove logfile   

     echo("<b>Success: ".$value."</b><br />"); 
    } 
} 

echo("Finished all file uploading."); 
echo "Last file: ".$filename; 
+0

謝謝。它似乎這就是我需要的,只是與文件名的問題,因爲文件名是'3D-neon-fox.jgg'和上傳文件名後echo是'-neon-fox.j' –

+0

@TemurPipia哦,我的錯。查看preg_match並將{3}更改爲{1} –

+0

謝謝安德烈,很棒的提示! :)並感謝所有人的幫助! –

-1

而是使用wget這樣的,你可以做到這一切使用捲曲,如果是可用的。

<?php 

set_time_limit(0); 

$lastDownloadFile = null; 
foreach ($_POST['store'] as $value) { 
    if ($value !== '' && downloadFile($value)) { 
     $lastDownloadFile = $value; 
    } 
} 

if ($lastDownloadFile !== null) { 
    // Print out info 
    $onlyfilename = pathinfo($lastDownloadFile, PATHINFO_BASENAME); 
} else { 
    // No files was successfully uploaded 
} 

function downloadFile($filetodownload) { 
    $fp = fopen(pathinfo($filetodownload, PATHINFO_BASENAME), 'w+'); 
    $ch = curl_init($filetodownload); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
    curl_setopt($ch, CURLOPT_FILE, $fp); // We're writing to our file pointer we created earlier 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Just in case the server throws us around 
    $success = curl_exec($ch); // gogo! 

    // clean up 
    curl_close($ch); 
    fclose($fp); 

    return $success; 
} 

一些謹慎的話讓人們上傳任何東西可能不是最好的主意。你想用這個完成什麼?

+1

就像我是cURL的粉絲一樣,問題是顯示循環中最後一個文件的URL,而這似乎並沒有解決這個問題。 – Quentin

+0

非常真實,也應該補充一點。註釋區域較小以顯示處理文件讀取的更好方法。 – hank

+0

@hank,關於爲什麼我讓人們將他們的內容上傳到網站,我一直在尋找像MFHS這樣的東西,但只是爲了上傳URL並找到我在上面提到的腳本altforge:http://sourceforge.net/projects/remotephpgrab/? source = dlp –