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.");
?>

上传文件后,我想显示文件的直接网址:例如

完成所有文件上传,直接URL: http ://site.com/files/grabbedfile.zip

你能帮我如何确定这段代码中最后上传文件的文件名吗?

提前致谢

4

2 回答 2

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;
于 2013-05-19T10:35:25.167 回答
-1

如果可以的话,您可以使用 cURL 来完成所有操作,而不是像那样使用 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;
}

但是,请注意,让人们将任何内容上传到您的服务器可能不是最好的主意。你想用这个来完成什么?

于 2013-05-19T09:47:43.760 回答