[英]ssh2_scp_send() returning "Failed to open stream: No such file or directory"
如果不是 ssh2_scp_send() 方法,今天我正在與 ssh 作斗爭並取得巨大成功。 這是我第一次將 ssh 與 PHP 集成,因此您可能會在我尚未發現的代碼中發現多個錯誤,因為我目前停留在該部分。
我正在使用一個虛擬數組來模仿我必須從幾個表單中獲得的一個,並且身份驗證工作正常,盡管我在那里遇到了一個小問題,因為我必須使用“root”作為用戶才能識別公鑰比較時正確,而不是我的實際用戶。 一旦我解決了那個問題,我發現代碼運行不正常,這次控制台說:
ssh2_scp_send(tmp/sshPm/prueba1.csv):無法打開流:第 32 行的 /home/josemaria/Desktop/API UBcloud/CSV/provisioning.php 中沒有這樣的文件或目錄
這是我的代碼:
function arrayToCsv($array, $filename, $delimiter = ';'){
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'";');
$f = fopen('/tmp/sshPm/'.$filename, 'r');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}
}
function connectSsh($filename){
$sshConnection = ssh2_connect($host, $port);
if (!$sshConnection) {
die('Conexión Fallida');
}
ssh2_auth_pubkey_file($sshConnection, $user, $pubKey, $priKey);
ssh2_scp_send($sshConnection, "/tmp/sshPm/$filename", '/home/scripts/CSV', 0644);
$stream = ssh2_exec($sshConnection, "/home/scripts/sc_prov_ovpn_firm.sh $filename");
ssh2_exec($sshConnection, 'exit');
return $stream;
}
for ($i=0; $i < 10; $i++) {
$array[] = array(
"VAAAAAGH",
"THE WAGH IS HERE",
200 + $i,
564451 +$i,
"sip",
"",
"",
"",
"8.8.8.8",
"8.8.4.4",
20048,
"Modelo Terminal",
"677shdG3"
);
}
$filename = 'test.csv';
arrayToCsv($array, $filename);
$stream = connectSsh($filename);
print_r($stream);
如您所見,我打算在 /tmp 中創建和存儲 CSV。 即使 csv 被創建並放置在正確的目錄中,每當我到達 scp_send 時,這種方法證明無法找到它。 我不知道這是否與我使用 root 驗證我的公鑰這一事實有關,因為我已經看到它應該是您登錄的用戶。
我也立即收到以下警告,但我想這是第一個的后果......無論如何,這里是:
PHP 警告:ssh2_scp_send():無法讀取第 32 行 /home/josemaria/Desktop/API UBcloud/CSV/provisioning.php 中的源文件。
我曾嘗試使用包裝器代替 fopen() 但沒有成功。 正如我所說,這是我第一次使用 ssh 和 PHP 工作,所以我會請你至少解釋一下! 十分感謝你的幫助!
更新
我設法通過遵循 ArSeN 的建議並在 /Desktop 中創建一個目錄並將所有路由更改為該目錄而不是 /tmp 來部分解決該問題。 現在我面臨的問題是我不確定一旦創建文件應該放在哪里。 所以我與這個問題相關的下一個問題是:
我應該在哪里存儲本地生成的所有 CSV? 如您所見,我在 /Documents 中執行此操作,因為我在那里沒有讀取/修改的限制,但我會說答案在於 /,也許是 /var? 我真的對這些東西一無所知......
再次感謝您提供的幫助!
這就是我的代碼現在的樣子:
function arrayToCsv($array, $filename, $delimiter = ','){
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'";');
$f = fopen("/home/josemaria/Documents/sshPm/$filename", 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}
fclose($f);
}
function connectSsh($filename){
$sshConnection = ssh2_connect($host, $port);
if (!$sshConnection) {
die('Conexión Fallida');
}
ssh2_auth_pubkey_file($sshConnection, $user, $pubKey, $priKey);
ssh2_scp_send($sshConnection, "/home/josemaria/Documents/sshPm/$filename", "/home/scripts/CSV/$filename", 0644);
$stream = ssh2_exec($sshConnection, "/home/scripts/sc_prov_ovpn_firm.sh $filename");
ssh2_exec($sshConnection, 'exit');
return $stream;
}
for ($i=0; $i < 10; $i++) {
$array[] = array(
"VAAAAAGH",
"THE WAGH IS HERE",
200 + $i,
564451 +$i,
"sip",
"",
"",
"",
"8.8.8.8",
"8.8.4.4",
20048,
"Modelo Terminal",
"677shdG3"
);
}
$filename = 'test.csv';
arrayToCsv($array, $filename);
$stream = connectSsh($filename);
print_r($stream);
您應該關閉文件處理程序,以便文件確實被寫入並且不在某些 I/O 緩沖區中。
function arrayToCsv($array, $filename, $delimiter = ';'){
// all your existing code here ...
fclose($f);
}
同樣,對於您的復制目標,您似乎將文件放在之前的文件夾中,這意味着:
ssh2_scp_send($sshConnection, "/tmp/sshPm/$filename", '/home/scripts/CSV', 0644);
應該是:
ssh2_scp_send($sshConnection, "/tmp/sshPm/$filename", "/home/scripts/CSV/$filename", 0644);
我的問題的答案與權限有關。 正如@ArSeN 指出的那樣,我還有一些其他錯誤,但是我得到的警告阻止了代碼工作是因為我試圖在 /tmp 中存儲和讀取文件,而沒有這樣做所需的權限。 因此,給自己對該目錄的完全權限或更改另一個目錄。 這段代碼仍然存在一些問題和問題,但我確實覺得它們屬於一個單獨的問題,我將在此處鏈接: 如何使用 PHP 和 ProcessMaker 在 Win 和 Linux 上生成 CSV? 建議使用哪些路徑在本地存儲文件?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.