I’m trying to SFTP a HTML file made on the fly(using file put contents) and the shh2 library.
Here’s what I’ve got so far, apache reports that it can’t be sent as the file doesn’t exist on the local disk:
$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);
The ssh2_scp_send() function requires the file path for the local and remote files.
http://php.net/manual/en/function.ssh2-scp-send.php
The file_put_contents() function returns the number of bytes written to the file, not the file path.
http://php.net/manual/en/function.file-put-contents.php
Therefore, your ssh2_scp_send() should read something as follows where the target directory on your target server is /tmp:
I don’t think it is possible to upload a file that does not exist yet as you would essentially be writing directly to the target server as opposed to uploading a file.
I would simply delete the file after sending using the unlink() function.
http://php.net/manual/en/function.unlink.php
So your complete logic would be as follows: