How to Write to a Remote File in PHP


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 ("", 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 "".

  • 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.

Related Searches


Promoted By Zergnet


You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!