【问题标题】:PhP Curl SFTP File EditingPhP Curl SFTP 文件编辑
【发布时间】:2018-08-08 21:53:51
【问题描述】:

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

<?php

$user="user";
$pass = 'pass';
$c = curl_init("sftp://$user:$pass@0.0.0.0/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】:

    首先,如果它是一个包含某种编程语言的文件,请检查 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多