我正在一個單獨的系統,需要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);
不知道我失去了什麼。想知道是否有與我忽略的這個程序有關的時間。任何幫助?
剛剛從URI去除這一部分,我覺得你的捲曲的代碼將工作:'$ ftp_user_name:$ ftp_user_pass @' – hanshenrik