使用PHP从远程VPS获取最后修改的文件

时间:2015-06-07 12:36:42

标签: php

使用此代码我可以获得修改时间,但我需要获取最后修改的文件名,所以有人可以告诉我如何做到这一点吗?

function filemtime_remote($uri)
{
    $uri = parse_url($uri);
    $handle = @fsockopen($uri['host'],80);
    if(!$handle)
        return 0;

    fputs($handle,"GET $uri[path] HTTP/1.1\r\nHost: $uri[host]\r\n\r\n");
    $result = 0;
    while(!feof($handle))
    {
        $line = fgets($handle,1024);
        if(!trim($line))
            break;

        $col = strpos($line,':');
        if($col !== false)
        {
            $header = trim(substr($line,0,$col));
            $value = trim(substr($line,$col+1));
            if(strtolower($header) == 'last-modified')
            {
                $result = strtotime($value);
                break;
            }
        }
    }
    fclose($handle);
    return $result;
}

function get_data($url) 
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}   

$result = "1";

$html = get_data("http://mysite.ir/api.php?uid=". $result);  //the result is r1&r2&r3&r4&....
$matches[1] = explode("&", $html);

if(!empty($matches[1])){
    $errorMessage .= "DONE";

    //Need Help here
    $last_modified_file = filemtime_remote($matches[1]);  //its just return modification time i need the last modified file name!!

}

1 个答案:

答案 0 :(得分:0)

除非远程服务器提供允许您为其提供文件名的脚本并将mtime返回给您从本地文件系统获取它,否则不能这样做。

如果有可能就这样恕我直言。