SFTP文件没有完整地下载与ssh2.sftp和fread

问题描述:

我正在一个单独的系统,需要SFTP查看日志的日志文件下载。我可以查看服务器上的可用日志文件并下载它们。我的问题是,似乎下载停止在2K,在大多数情况下,它只是日志文件的前10行。这些文件应该包含数千行,因为它们是对系统所做更改的每日日志。SFTP文件没有完整地下载与ssh2.sftp和fread

我已经在两个单独的文件这样做,一个加载的所有文件到一个页面,用户可以选择可用的日志文件,然后单击链接在浏览器中查看的内容:

$ftp_server = "IP of Server"; 
$ftp_user_name = "USERNAME"; 
$ftp_user_pass = "PASSWORD"; 

$connection = ssh2_connect($ftp_server, 22); 
ssh2_auth_password($connection,$ftp_user_name, $ftp_user_pass); 
$sftp = ssh2_sftp($connection); 

$dh = opendir("ssh2.sftp://$sftp//EMSftp/audit_files/"); 

while (($file = readdir($dh)) !== false) { 
    if ($file != '.' && $file != '..'){ 
     if (strpos($file,"2017-04")){ 
      echo '/EMSftp/audit_files/'.$file.' | <a href="open_file_curl.php?file='.$file.'" target="_blank">curl</a> 
               | <a href="open_file.php?file='.$file.'" target="_blank">view</a><br>'; 
     } 
    } 
} 

closedir($dh); 

我试图用sftp和ssh2以两种不同的方式下载文件:

$file = $_GET['file']; 
$local_file = "/var/www/uploads/logs/$file"; 

if (!file_exists($local_file)){ 
    $connection = ssh2_connect($ftp_server, 22); 
    ssh2_auth_password($connection,$ftp_user_name, $ftp_user_pass); 
    $sftp = ssh2_sftp($connection); 

    $stream = @fopen("ssh2.sftp://$sftp//EMSftp/audit_files/$file", 'r'); 

    if (! $stream) 
     throw new Exception("Could not open file: $remote_file"); 
    $contents = fread($stream, filesize("ssh2.sftp://$sftp//EMSftp/audit_files/$file"));    
    file_put_contents ($local_file, $contents); 
    @fclose($stream); 
} 

echo '<pre>'; 
echo file_get_contents($local_file); 
echo '</pre>'; 

并且还尝试使用curl来完成此操作。这只会创建一个空白文件。不知道这里缺少什么。无法将文件内容添加到文件。

$file = $_GET['file']; 

$local_file = "/var/www/uploads/logs/$file"; 

fopen($local_file, 'w+'); 
chmod($local_file, 0777); 

$remote = "sftp://$ftp_user_name:[email protected]$ftp_server//EMSftp/audit_files/$file"; 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $remote); 
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_SFTP); 
curl_setopt($curl, CURLOPT_USERPWD, "$ftp_user_name:$ftp_user_pass"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$somecontent = curl_exec($curl); 
if (is_writable($local_file)) { 

    if (!$handle = fopen($local_file, 'a')) { 
     echo "Cannot open file ($local_file)"; 
     exit; 
    } 

    if (fwrite($handle, $somecontent) === FALSE) { 
     echo "Cannot write to file ($local_file)"; 
     exit; 
    } 

    echo "Success, wrote ($somecontent) to file ($local_file)"; 

    fclose($handle); 

} else { 
    echo "The file $local_file is not writable"; 
} 
curl_close($curl); 

不知道我失去了什么。想知道是否有与我忽略的这个程序有关的时间。任何帮助?

+0

刚刚从URI去除这一部分,我觉得你的卷曲的代码将工作:'$ ftp_user_name:$ ftp_user_pass @' – hanshenrik

这是错误的:

$contents = fread($stream, filesize("ssh2.sftp://$sftp//EMSftp/audit_files/$file")); 

它并不能保证你,那整个文件将被读取。

由于documented

fread()读取高达length字节


如果日志是合理的规模,使用简单的file_get_contents

$contents = file_get_contents("ssh2.sftp://$sftp//EMSftp/audit_files/$file"); 

如果没有,请阅读数据块文件中的一个循环:

$stream = @fopen("ssh2.sftp://$sftp//EMSftp/audit_files/$file", 'r'); 

while (!feof($stream)) 
{ 
    $chunk = fread($stream, 8192); 
    // write/append chunk to a local file 
} 

@fclose($stream); 
+0

完美,这似乎是工作,我要验证明天是否在系统管理员中获取整个文件。 –