I have a script on my one server and I want that script to create a file on another server of mine using PHP, NOT VIA FTP?
There are many ways to do this. I'd pick the first one myself because it's easiest to set up:
If you have PHP+Apache on another server, just call some script on the other server using file_get_contents with http URL as filename or use cURL if you need to POST file contents as well.
If the servers are in same network (LAN, VPN) you can use Windows shares/Samba or NFS to mount a remote directory to you local filesystem and simply write to file directly using fopen/fwrite functions
Use SSH via SCP or SFTP
PHP allows sending files across SSH - see the ssh2* family of functions, in particular ssh2_scp_send and ssh2_scp_recv.
I've never used them myself, but the infrastructure is there in Linux, just like SMB in Windows.
In general, FTP is the only regularly and easily available way (in PHP) to create a file on another server.
There are of course other protocols that enable you to create a file, but they all require installation of software on either one or both servers:
Samba (would enable access to the remote server through an absolute file path)
WebDaV (PHP client libraries available)
SCP (Finding a PHP client is probably going to be hard)
If both servers run PHP, it's probably the easiest to set up a PHP script on the remote server that accepts file data trough POST, and writes it out to a local file. Not a perfect solution, though, due to the limits usually imposed on POST uploads.
You could always use DAV, but it might require some configuration on the receiving server. There is also SSHFS, which lets you easily mount the remote directory locally over a SSH tunnel, or just use the ssh2_* family of functions as Andy Shellam suggested.
Really, there are lots of ways to accomplish this.
Related
I wanted to if there is any real difference using PHP sftp for file open like shown here and with ssh2_scp_send I am planning on uploading multiple files per php session and want to not spend to much time in uploading the files. If anyone is aware on how these to functions are implemented in PHP it would be great to know so i can choose the right one.
Thanks Again.
The SFTP and the SCP are different protocols. Both run over the SSH though.
The SCP can only copy (upload/download) files. It cannot do any other operation like listing directory contents, deleting files etc.
The SFTP is a full-fledged remote fie system protocol.
The SCP might be quicker in general, as it is better able to utilize the SSH virtual connection channel. But it's worth testing both.
Also note that the SCP will generally work against *nix SSH servers only. The SFTP is more universal.
For details on The SFTP + SCP refer to:
https://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol
https://en.wikipedia.org/wiki/Secure_copy_protocol
ssh2_sftp() is used on a existing connection, then you use fopen() however, this is also considered 'insecure' by most. Which is where ssh2_scp_recv() uses the Secure Copy based on BSD RCP Protocol. SCP uses Secure Shell (SSH) for data transfer and uses the same mechanisms for authentication, thereby ensuring the authenticity and confidentiality of the data in transit. A client can send (upload) files to a server, optionally including their basic attributes (permissions, timestamps). Clients can also request files or directories from a server (download). SCP runs over TCP port 22 by default. Like RCP, there is no RFC that defines the specifics of the protocol.
http://www.php.net/manual/en/function.ssh2-scp-recv.php
http://en.wikipedia.org/wiki/Secure_copy
I want to write a PHP script that connects to a network PC's C drive: \MAINPC\C$ and then authenticates itself (I provide the username and password) and then iterate over the file in another folder. The iterating part is easy, but I am stuck as to what to use to actually connect to the Windows server or whether or not I would have to use some low-level socket connection?
There is this:
stream wrapper to access Windows shared files
It wraps the smbclient program.
Further discussion in this SO question.
You can mount network shares to device names in windows, or simply mount them to a directory (via smb) on linux.
Then to php it is a simple file operation. You will have to make sure that the share is up tho.
PHP cannot magically access data on another machine.
You need a server on that machine (here the windows PC) that serves the file system you are interested in. Then you can create a php script that accesses the content of the filesystem by using that server.
Actually there are ready-to-use components for that stuff.
Is the webserver on a linux/unix-box?
Are the machines on the same LAN?
http://www.samba.org/samba/docs/man/manpages-3/smbclient.1.html may help you.
I am attempting to fwrite() via an FTP wrapper from a UNIX-based server to a Windows-based server with PHP. I am connecting successfully, and am able to (for example) create directories. However, I can't seem to write files to the folders! When I look at the directory permissions using an FTP client, I noticed that they are all 0000, and cannot be changed. Apparently, Windows-based servers do not use the same FTP permission system.
So... what's up? What do you recommend? I am able to upload files to the directory using an FTP client, but when I attempt to write files with PHP, nothing happens.
I am at a complete loss as to why this may be occurring. I have confirmed my script works by writing to UNIX-based servers, so that is not a problem. Is it possible that the fact the destination server does not have PHP installed matters? I would not think so, but I'm open to any ideas at this point!
Thank you!
EDIT - What's really getting me is that I AM able to create directories, so it doesn't make any sense that writing shouldn't be working. On Windows servers, is there some setting that would prevent just the writing of FILES, but not folders?
EDIT 2 - More research has told me that, though you cannot CHMOD on Windows servers, PHP's CHMOD still somehow does something with the permissions. However, this does not appear to be working for me. Is there a way to change the permissions directly with PHP code, or is this something that has to be done directly on the server, outside of my reach?
I recommend file_put_contents, and make sure PHP is allowed to write in that directory.
Is there any way to have one server call and execute a script on another server? I have tried ftp_exec(), but the server does not support it. Here is what I am trying to do.
Server 1 creates and uploads a zip file to Server 2. Server 2 then unzips and extracts the file.
Currently I have scheduled cron jobs at alternating times, one on each server in order to do this. Ideally I would like Server 1 to be able to do everything, sending a message to sever 2 telling it to unzip and extract the uploaded file.
Is it possible to do something on Server 1, like exec(php ftp://user:password#server2/unzip.php) ?
Is it maybe possible using CURL?
FTP = File Transfer Protocol. It's not intended (and should never be used for) remote execution. If you need to trigger a remote script, use HTTP. It's easy enough to do
$stat = file_get_contents('http://example.com/unzip.php');
to invoke the remote PHP script to do the unzipping. If you need authentication on the URL, you can set up a stream or use CURL instead.
Does your sever support ssh or have you the ssh account?
You can run remote commands via ssh.
Im running a file host thats grown beyond the capacity of a single server, and I need to implement multiple server storage of files. I'd like to do this as cheap as possible, so those fancy mass storage methods are out of the question. I simply want to move a file thats uploaded by the user to the "gateway" server, which hosts all the http and mysql, to one of the media servers. It can either be done at the end of the user's request, or via cron every couple of minutes.
At this point the only method Im truly familiar with, is using ftp_put php function, and simply ftping the file to a different server, but I've had problems with this method in the past, especially for larger files, and a lot of the files that will be getting transferred will be over 100mb.
Can anyone suggest a good solution for this? Preferably Im looking for a purely software solution... hopefully nothing more than a php/bash script.
One method that requires no programmatic setup, and is quite secure is to mount a folder from the other server, and use php to save to that directory like you would normally.
You don't have to use sshfs, there are a bunch of other ways to provide the same solution. I would use sshfs for this situation as it works across ssh, and therefore is secure and relatively easy to setup.
To achieve this (using sshfs):
Install sshfs
Mount a ssfs file to a folder accessable to php
Use a php script to store the files within the sshfs mount, and therefore on the other server.
Another method is to setup an rsync command between the two using crontab.
you can write a bash script that run in cron
and use command line like scp or sftp or rsync
example :
[bash]# scp filename alice#galaxy.example.com:/home/alice
Copy "filename" into alice's home directory on the remote galaxy.example.com server
Use cron, a bash script, and rsync.
cron: "Cron is a time-based job scheduler in Unix-like computer operating systems. 'cron' is short for 'chronograph'."
bash: "Bash is a free software Unix shell written for the GNU Project."
rsync: "rsync is a software application for Unix systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate."
In summary, place the rsync command in a bash script and get cron to run it at regular intervals.
See here for some examples: