简体   繁体   中英

how to open and write texfile to remote server's specific location in PHP

I am currently opening and writing a text file into my local server with the following:

$mypath="sms_file\\cbsms_";
$fp = fopen($file_name.'.txt', "w");
fwrite($fp, $value. "\r\n");
fclose($fp);

I want to now copy that file to a remote server like /home/project on 10.10.18.23 (home network)

Assuming that I have R/W access in that directory, what would be the best way of achieving this?

The remote server needs to know that there is a request coming in to store a file on it. There are several possibilities here, the easiest would be to run a FTP server.

Another option would be to use the exec() function call scp on the command line (provided you have exchanged ssh keys with the remote server).

Another option would be to create a PHP page on the remote server that accepts POST requests with files and stores them. You must provide your own security measures in this case.

If you can mount the remote host as a permanent volume (via NFS or CIFS), you can use the regular PHP copy() function.

You can try using exec() to run an SCP command:

exec('scp /path/to/file.txt user@homenetworkhost:/home/project/file.txt');
// Obviously, you'll have to set up your SSH permissions and for 'user@homenetworkhost' you'll want to change it to your home network's user and host names.

By the looks of your exmample, I would say your PHP server is on Windows (looking at the backslash in $mypath="sms_file\\\\cbsms_"; ), and your remote host is UNIX/LINUX (looking at forward slashes and location /home/project ). I would suggest setting up SSH or FTP on the remote host and rather use those protocols than copying it to a network location. Your PHP server (Windows box) will then have to communicate via SSH/FTP and copy the file.

References: http://php.net/manual/en/book.ftp.php http://php.net/manual/en/function.ssh2-scp-send.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM