You can use PHP to write a file to a remote server. This can be helpful for purposes such as logging or using your PHP script to dynamically save information. You must have the proper permissions on the remote server you are trying to write a file to. PHP has several built-in functions, such as "fopen" and "fwrite," that you can use in your PHP file to connect to the remote server, create a file, write to it and then save it.
Open your PHP file in a text editor, such as Windows Notepad.
Connect to a remote FTP server. Open a file with the "fopen(address, mode)" command. The mode needs to be set to "w" for writing. For example, "$my_file = fopen ("ftp://ftp.myserver.com/myfile.txt", w);" will anonymously connect you to the ftp server and create a new file called "myfile.txt" that you can write to. To use a non-anonymous account, change the address to use the format "ftp://username:firstname.lastname@example.org/file".
Write data to the file using the "fwrite(handle, string);" command. For example, "fwrite ($my_file, "my text");".
Close your connection once you are done writing to the file with the "fclose(handle);" command. For example, "fclose($my_file);". Once executed, the new file will now have been written to and saved on the remote server.
Save the PHP file.
Tips & Warnings
- PHP code must be contained within "<?php" and "?>" tags.
- It's not possible in PHP to directly write to a pre-existing remote file.
How to Copy Files From Your Remote Desktop Session Without FTP
Have you ever need to copy files back and forth when you are in the middle of a remote desktop session. That...