Пытаюсь создать и передать файл xml c содержимым через SSH Пробовал через curl,при этом файл создается на sftp но с пустым содержимым. Сам код:
$xm = "test text";
$ch = curl_init();
$file = $xm;
$remotefile = 'report-01.xml';
$fp = fopen($file, 'r');
fwrite($fp,$file);
fclose($fp);
curl_setopt($ch, CURLOPT_URL, 'sftp://login:pass@adressserver/1/'.$remotefile);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
Второй вариант я пробовал так, но файл вообще не передавало. код:
$copycheck="c;";
$text=$copycheck;
$fp = fopen ("ssh2.sftp://login:pass@adressserver/1/filename.xml", "w");
fwrite($fp,$text);
fclose($fp);
Подскажите где я допустил ошибку, или возможно есть еще варианты.
Вариант с использованием временного файла:
<?php
$xm = "test text";
$file = tempnam(sys_get_temp_dir(), "foo");
file_put_contents($file, $xm);
$ch = curl_init();
$remotefile = 'report-01.xml';
$fp = fopen($file, 'r');
curl_setopt($ch, CURLOPT_URL, 'sftp://login:pass@adressserver/1/'.$remotefile);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_exec ($ch);
fclose($fp);
unlink($file);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
echo $error;
Продвижение своими сайтами как стратегия роста и независимости