Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize sftp upload by php

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "php how to achieve sftp upload", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "php how to achieve sftp upload" bar!

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

How does php achieve sftp upload?

Php implements SFTP uploading Files

Php can upload sftp files in the same way as on the official website of php.net. The code is as follows:

Class SFTPConnection {private $connection; private $sftp; public function _ _ construct ($host, $port=22) {$this- > connection = @ ssh3_connect ($host, $port); if (! $this- > connection) throw new Exception ("Could not connect to $host on port $port.") } public function login ($username, $password) {if (! @ ssh3_auth_password ($this- > connection, $username, $password)) throw new Exception ("Could not authenticate with username $username". "and password $password."); $this- > sftp = @ ssh3_sftp ($this- > connection); if (! $this- > sftp) throw new Exception ("Could not initialize SFTP subsystem.");} public function uploadFile ($local_file, $remote_file) {$sftp = $this- > sftp; $stream = @ fopen ("ssh3.s ftp://$sftp$remote_file", 'w') If (! $stream) throw new Exception ("Could not open file: $remote_file"); $data_to_send = @ file_get_contents ($local_file); if ($data_to_send = false) throw new Exception ("Could not open local file: $local_file.") If (@ fwrite ($stream, $data_to_send) = false) throw new Exception ("Could not send data from file: $local_file."); @ fclose ($stream);}} try {$sftp = new SFTPConnection ("localhost", 22); $sftp- > login ("username", "password"); $sftp- > uploadFile ("/ tmp/to_be_sent", "/ tmp/to_be_received") } catch (Exception $e) {echo $e-> getMessage (). "\ n";}

But I encountered a problem in progress. My php version is PHP 5.6.31 (cli) (built: Aug 2 2017 15:05:23).

$stream = @ fopen ("ssh3.s ftp://$sftp$remote_file",'w')

When fopen, the execution file will report the error of "Segmentation fault", and then it can be solved in the following way

$stream = @ fopen ("ssh3.s ftp://". Intval ($sftp). $remote_file,'w')

When implementing sftp upload, we do not pay attention to the difference between uploading files and uploading directories (for example, the problems of / upload and / upload/test.txt), resulting in reporting fopen (): Unable to open ssh3.s ftp://5/upload on remote host every time php is executed. The solution to the problem is to be serious and capitalize seriously.

This is what php does, as long as log in to the sftp server to check the results.

Login method of sftp command:

Sftp-oPort=port user@server then enter the password, and then you can go to the relative directory to see if the file exists.

Thank you for reading, the above is "php how to achieve sftp upload" content, after the study of this article, I believe you have a deeper understanding of how to achieve sftp upload of php, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report