【问题标题】:how to SFTP get file using cURL with PHP如何使用 cURL 和 PHP SFTP 获取文件
【发布时间】:2013-06-11 18:24:43
【问题描述】:

我正在尝试使用 PHP 中的 cURL 库从远程服务器 SFTP 获取文件,如下所示。我收到 no such file exists 错误。任何想法我做错了什么?

<html>
<body>

<?php
$user="username";
$pass="password";
$filename="/opt/vmstat/vmstat_file";
$c = curl_init("sftp://$user:$pass@server1/$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

【问题讨论】:

  • 你确定 sftp://$user:$pass@server1/$filename 吗?或者也许更好 sftp://$pass:$user@server1$filename ?
  • 所写的代码是正确的。我怀疑奥卡姆剃刀有效……文件不存在。
  • 文件确实在那里。
  • 检查文件所有权和权限
  • 该文件作为每个人的 r 权限。

标签: php curl sftp


【解决方案1】:
$c = curl_init("sftp://$user:$pass@server1/opt/vmstat/vmstat_file");
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
$data = curl_exec($c);
curl_close($c);

这行得通。

【讨论】:

  • 您说这是作为一种解决方案(即不再将文件名作为变量)还是作为故障排除步骤(即当您自己输入文件名时它起作用)?
  • 也许问题是问题代码中的双斜杠。 "server1//opt/vmstat/vmstat_file" != "server1/opt/vmstat/vmstat_file"
【解决方案2】:

检查文件所有权和权限。 你正在做一个fopen($filename, 'w')。你只想说fopen($filename, 'r')吗?

【讨论】:

  • 我意识到错误,将其更改为 r,仍然没有文件存在错误。
  • 文件是在远程服务器上还是在运行代码的盒子上? fopen 正在尝试在本地服务器上打开文件
【解决方案3】:

我认为问题在于您正在打开本地文件句柄进行读取。

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

我拿走了你的代码并将“r”更改为“w”,它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多