2013-06-11 19 views
-1

我嘗試使用PHP中的cURL庫來從遠程服務器獲取SFTP文件,如下所示。我收到一個no such file exists錯誤。任何想法我做錯了什麼?如何使用cURL與PHP獲取文件

<html> 
<body> 

<?php 
$user="username"; 
$pass="password"; 
$filename="/opt/vmstat/vmstat_file"; 
$c = curl_init("sftp://$user:[email protected]/$filename"); 
$fh = fopen($filename, 'r') or die($php_errormsg); 
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP); 
curl_setopt($c, CURLOPT_FILE, $fh); 
curl_exec($c); 
curl_close($c); 

?> 

</body> 
</html> 

錯誤:

Warning: fopen(="/opt/vmstat/vmstat_file): failed to open stream: No such file or directory in C:\inetpub\wwwroot\sftp.php on line 9 
+0

你確定sftp:// $ user:$ pass @ server1/$ filename?或者更好的sftp:// $ pass:$ user @ server1 $ filename? – Eugen

+0

同樣的錯誤。 – user1471980

+2

代碼正確無誤。我懷疑Occam的Razor實際上...文件不存在。 –

回答

2
$c = curl_init("sftp://$user:[email protected]/opt/vmstat/vmstat_file"); 
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP); 
$data = curl_exec($c); 
curl_close($c); 

這工作。

+1

你是說這是一個解決方案(即不再有文件名作爲變量)還是作爲一個故障排除步驟(即它在你自己輸入文件名時起作用)? – nhinkle

+0

也許問題是問題代碼中的雙斜槓。 「server1 // opt/vmstat/vmstat_file」!=「server1/opt/vmstat/vmstat_file」 –

0

檢查文件所有權和權限。 您正在做一個fopen($filename, 'w')。你只想說fopen($filename, 'r')而不是?

+0

我意識到錯誤,把它改爲r,仍然沒有文件存在錯誤。 – user1471980

+1

遠程服務器上的文件或您在其上運行代碼的文件夾中的文件是? fopen試圖打開本地服務器上的文件 – ForOhFor

0

我認爲問題在於你打開本地文件句柄進行閱讀。

$fh = fopen($filename, 'r') or die($php_errormsg); 

我把你的代碼和'r'改爲'w',它適用於我。