PhP卷曲SFTP文件编辑

问题描述 投票:0回答:1

我正在尝试为多个服务器创建一个在线编辑器。我想在服务器上编辑自定义文件,我需要通过sftp获取它。我当前的代码如下所示:

<?php

$user="user";
$pass = 'pass';
$c = curl_init("sftp://$user:[email protected]/path/to/file/file.txt");
curl_setopt($c, CURLOPT_PORT, 3206);
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($c, CURLOPT_FILE, $fh);
curl_exec($c);
curl_close($c);
//the next line is not working and from now on am I stuck
$text = file_get_contents($fh);

?>
<!-- HTML form -->
<form action="" method="post">
<textarea name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input 

type="submit" />
<input type="reset" />
</form>

我想在网站上编辑此文件,然后将其上传到同一目录中的sftp服务器(覆盖现有文件)。我不知道如何继续。谢谢您的帮助。

php html curl web sftp
1个回答
0
投票

首先,如果它是包含某种编程语言的文件,请检查ACE.js.它只是一个令人难以置信的JS模块,可用作Web IDE,它具有任何程序员在IDE中寻找的所有功能,它非常好,我几乎会考虑切换到它作为我的主IDE。

然后使用这个PHP代码:

<?php
  $_POST = json_decode(file_get_contents('php://input'), true);
  $filename = 'sftp://user@location/file/name.js';
  //Use SSH public key authentication
  $handle = fopen($filename,'w') or die('Cannot open file: '.$filename);
  $data = $_POST['src'];
  fwrite($handle, $data);
  fclose($handle);
?>

并使用此JS代码来调用PHP脚本:

<script src="../scripts/ace/ace.js" type="text/javascript"></script>
<script>
  var editor = ace.edit("editor");
  editor.setTheme('<?php echo $theme; ?>');
  editor.getSession().setMode("<?php echo $language; ?>");
  editor.setShowPrintMargin(false);
  editor.setReadOnly(true);
  <?php //Save shortcut binding ?>
  editor.commands.addCommand({
    name: 'Save',
    bindKey: {win: 'Ctrl-S',  mac: 'Command-S'},
    exec: function(editor) {
      var xhr = new XMLHttpRequest();
      xhr.open("POST", './scripts/save_file.php', true);
      xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
      xhr.send(
        JSON.stringify(
            {src:editor.getValue()}
        )
      );
    }
  });
</script>
© www.soinside.com 2019 - 2024. All rights reserved.