tags:

views:

413

answers:

5

I need to write to a file that is located on a different server using php.

So, I get user data from a form on one server, and then need to write to a text file that is located on another server. Do I need to authenticate or something similar?

The second server that I need to write to is a windows server. Is this an issue?

Thanks!

A: 

with php you can make a ftp connection and write the file on the .net server with ftp

authentication can be done by ftp

for information how you can do this http://nl.php.net/ftp

Are you running ftp on the second server ?

You can also do a ajax call to the seconde server with the data

On the seconde server you place a script that writes the file with the content but then you must run http on the seconde server with a script language like php/asp

Marco
So would I use ftp_nb_fget ?
Joe
It depends on what you plan on doing, you can download a file from the FTP server using `ftp_nb_fget` and then write to it
Anthony Forloney
Yes, If you want to put a file to the windows server from the php server then you need ftp_nb_fput and if you will download a file from the windows server then use the fget method
Marco
So should i be writing to the file locally, then putting it on the server after wards? Is there an issue with overwriting the current file in this situation?
Joe
Yes write file local and then transfer it with ftp
Marco
Or write file on windows server if you choose a ajax call solution
Marco
+1  A: 

There is no issue whether you are writing to either servers, you can accomplish this with PHP FTP.

Make sure you have correct permissions to write files on the servers or your code will not work and get an access denied error.

Anthony Forloney
So would I use ftp_nb_fget ?
Joe
You could use that function, but I am wondering if you use it to access a non-existent file on the FTP, if it would create one for you.
Anthony Forloney
The file will always exist. I just need to open it, write to it, and then close it. Which php ftp function should i use for this?
Joe
`ftp_nb_fget` offers non-blocking asynchronous support so you can do what you need to while the file is writing.
Anthony Forloney
+3  A: 

You need to have some service on the remote (windows) machine that will accept the file and store it. There are many ways to do this:

  • a Windows share (if your first server is Linux, you can mount it via Samba or use smbclient to push the file. If it's windows, just assign it a letter like Z: and copy file there)
  • FTP (run some FTP server like FileZilla and use cURL or some other PHP library to upload)
  • HTTP (start Apache+PHP or IIS/.Net or whatever webserver on Windows and write a small program that will accept the POST data and store it into file. You can use cURL or some other library to use HTTP POST to send the file from PHP)
  • SSH (you can run OpenSSH server and copy the file with scp)

I use HTTP approach and it works quite well (because I already run Apache on the remote machines), but any other option is viable as well.

Milan Babuškov
A: 

If you want to use SFTP, you can do it using cURL. Stack Overflow post

eyelidlessness
+1  A: 

I don't know if this is a possibility for you but...

Rather than trying to update a file on another server, especially something simple like a txt file I would have the current file on server one and then I would copy it to server two from server two with cURL using a cron job set to copy the file every x minutes.

    $ch = curl_init(); 

    // set URL and other appropriate options 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    set_time_limit(3600); # 5 minutes for PHP 
    curl_setopt($ch, CURLOPT_TIMEOUT, 3600); # and also for CURL 

    $outfile = fopen( $dirname, 'wb'); 
    curl_setopt($ch, CURLOPT_FILE, $outfile); 

    // grab file from URL 
    $response = curl_exec($ch); 
    fclose($outfile); 

    // close CURL resource, and free up system resources 
    curl_close($ch); 

`

I would do this just because you would not have to deal with setting up a FTP account for the update or anything like that. Your server that you need the file on is going out and grabbing the file for itself.

I use the above script to collect mp3s from various blogs. Sometime they will be 100-200MB and it works fine running off a shared server.

ian
Thanks for the nice alternative. Two questions... you say "Your server that you need the file on is going out and grabbing the file for itself." so i would put the above code on the server that i want the file to end up on? The server that the file needs to end on is a windows box, and thus does not have php installed. Question 2... the setopt lines above: are these the correct settings for transferring a text file? Thanks!
Joe
Thats right. Think of it as downloading the txt file to the machine you want it on instead of uploading to the machine you want...I am sure you could do something similar with a windows based language. Or use WAMP but that complicated it again.Yes the setopt lines are just:The url to transferIf it should use a header or not (i think)The type of transfer Binary (so any kind of file)Return a response (for errors)
ian