我正在使用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
您能帮我解决一下如何在此代码中确定上次上传文件的文件名吗?
提前致谢
答案 0 :(得分: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;
答案 1 :(得分:-1)
不是像这样使用wget,而是可以使用cURL完成所有操作(如果有的话)。
<?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;
}
但是,有些人要谨慎,让人们将任何内容上传到您的服务器可能不是最好的主意。你想用这个来完成什么?