Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SFTP一种动态生成的HTML文件,用于外部服务器_Php_Html_Ftp_Sftp - Fatal编程技术网

Php SFTP一种动态生成的HTML文件,用于外部服务器

Php SFTP一种动态生成的HTML文件,用于外部服务器,php,html,ftp,sftp,Php,Html,Ftp,Sftp,我正在尝试SFTP一个动态生成的HTML文件(使用file-put-contents)和shh2库 以下是我到目前为止得到的信息,apache报告无法发送该文件,因为该文件在本地磁盘上不存在: $pageBody = '<body> <div id="canvas_container"> <canvas id="designer_canvas" width="430" height="415"> </canvas&

我正在尝试SFTP一个动态生成的HTML文件(使用file-put-contents)和shh2库

以下是我到目前为止得到的信息,apache报告无法发送该文件,因为该文件在本地磁盘上不存在:

$pageBody = '<body>
    <div id="canvas_container">
        <canvas id="designer_canvas" width="430" height="415">
        </canvas>
    </div>
<div style="display:none" id="share_design_details">
<li>'.$mc1.'</li><li>'.$mc1.'</li><li>'.$mc2.'</li><li>'.$sp.'</li><li>'.$p.'</li><li>'.$c.'</li></div>

<div id="test">This design is called : '.$designName.'</div></body>' ; 


$newFile = file_put_contents('newfile.html',$pageBody);
$connection = ssh2_connect('myhost.com', 22);
ssh2_auth_password($connection, 'myuser', 'mypass');

$sftp = ssh2_sftp($connection);
ssh2_scp_send($connection, $newFile, $newFile, 0644);
$pageBody='1!'
  • “.$mc1.”
  • “.$mc1.”
  • “.$mc2.”
  • “.$sp.”
  • “.$p.”
  • 。$c.'
  • 此设计称为:“.$designName”。”; $newFile=file\u put\u contents('newFile.html',$pageBody); $connection=ssh2_connect('myhost.com',22); ssh2_auth_密码($connection,'myuser','mypass'); $sftp=ssh2\U sftp($connection); ssh2_scp_send($connection,$newFile,$newFile,0644);
    ssh2\u scp\u send()函数需要本地和远程文件的文件路径

    file\u put\u contents()函数返回写入文件的字节数,而不是文件路径

    因此,您的ssh2\u scp\u send()

    我认为上传一个还不存在的文件是不可能的,因为与上传一个文件相比,您实际上是在直接向目标服务器写入文件

    我只需在发送后使用unlink()函数删除文件

    因此,您的完整逻辑如下所示:

    //Define content
    $pageBody = 'CONTENT HERE';
    
    //Create the file
    $newFile = file_put_contents('temp.html', $pageBody);
    
    //Connect to SFTP host
    $connection = ssh2_connect('myhost.com', 22);
    ssh2_auth_password($connection, 'myuser', 'mypass');
    $sftp = ssh2_sftp($connection);
    
    //Send the file
    ssh2_scp_send($connection, 'temp.html', '/tmp/temp.html', 0644);
    
    //Delete the local file
    unlink('temp.html);
    
    完全未经测试,但通常通过写入SSH连接(请注意,在shell下)使其可供另一方使用。记住这一点,运行一个命令,将其放置在另一侧的正确位置,然后对其进行写入

    $stream = ssh_exec('cat > some/file');
    $result = fwrite($stream, $pageBody);
    

    我自己会使用phpseclib,一个纯粹的phpstp实现。例如

    <?php
    include('Net/SFTP.php');
    
    //Define content
    $pageBody = 'CONTENT HERE';
    
    //Connect to SFTP host
    $sftp = new Net_SFTP('myhost.com', 22);
    $sftp->login('myuser', 'mypass');
    
    //Send the file
    $sftp->put('/tmp/temp.html', $pageBody);
    ?>
    
    
    

    更便于携带,也更易于使用。

    返回true或false,但ssh2\u scp\u send()需要一个文件路径。我如何修改send,以便它在不必在本地磁盘上创建文件的情况下上载该文件?我认为您不能。我会说,只需将文件名传递给它,并在事后将其删除。如果有办法,我会将其保持打开状态,但将其作为答案发布,因为它可能对其他人有用。请参阅示例#1通过SFTP at打开文件-只需打开文件进行写入而不是读取
    <?php
    include('Net/SFTP.php');
    
    //Define content
    $pageBody = 'CONTENT HERE';
    
    //Connect to SFTP host
    $sftp = new Net_SFTP('myhost.com', 22);
    $sftp->login('myuser', 'mypass');
    
    //Send the file
    $sftp->put('/tmp/temp.html', $pageBody);
    ?>