How to Write Contents to a File Using PHP

PHP



In this article, we show how to write contents to an existing file that you have in a directory on a server.

Say, you have an existing file already in a directory on your server and you want to write something to that file, such as a few lines.

Well, with PHP, you can write contents to a file without having to go that file and open it up and write the content.

Since PHP is a server side language, it can access any file in a directory on your server and write contents to it without you having to directly go to that file.

Before I go over to the PHP code, look at the file below.

This is the contents of the file, sample.txt.

, https://pcsites.pw/htdocs/index.php/view/17bde245 australia_online_chat_-_creating_a_dating_website, http://flynnos.org/flynnbin/ connecting_singles_dating_site_-_personal_dating, https://paste.feed-the-beast.com/view/c8de427e chat_sa_zgodnim_curama, https://pastebin.connexeon.com/view/50b52fdf recorded_sex_chat, https://bin.simulakrum.org/view/fcc835cd shy_girl_dating_guide, https://paste.nrecom.net/view/0e622023 ukrainian_russian_dating, https://paste.tuxcon.com/view/047eccf6 how_to_hook_up_2_amps_in_a_car_diagram, https://fulscript.nu/view/47e75dd0 holland_chat_room_online, https://paste.godclan.hu/view/qQRLW67l convert_local_date_to_datetime, http://p.os3.ca/view/e40ba326 free_nigerian_online_dating_sites, https://p.qdnx.org/view/a3168b3b importar_contactos_outlook_2013_csv, https://spit.mixtape.moe/view/f23167fb sexnoveller_milf_jobbet, https://fulscript.nu/view/4b301fde first_message_on_dating_site, http://p.os3.ca/view/707ba685 besplatni_oglasi_za_upoznavanje_twoo, https://pastebin.creamen.net/htdocs/view/163047ad how_to_hook_up_a_mixer_board_for_podcasting, https://p.qdnx.org/view/bda175a9 dating_after_divorce_for_men_-_long_hair_dating_site, http://paste.beautifulsunrise.org/view/9e2ea2b1 date_picker_excel_vba_userform, https://paste.godclan.hu/view/51kvLlP0 free_online_dating_in_pakistan_without_registration, http://paste.ainsey11.com/view/b3875f3f casual_dating_sites_free, https://paste.centos.org/view/a7510b5c licni_oglasi_on_trazi_nju_youtube, https://digitaldevices.ca/Stikked/ how_to_start_conversation_on_dating_site, http://txt.krzysiu.net/view/8fcc1d4f help_for_single_pregnant_women, http://paste.ainsey11.com/view/5b898d1a date_med_en_engel_roman_analyse, http://paste.purplehat.org/view/3ca0e13e devojke_za_brak_subotica, https://fulscript.nu/view/a95a9dd5 trazim_momka_za_intimno_druzenje, https://www.pastebin.es/view/f3493d09 gole_domace_devojke_gole, https://pastes.rehm.me/view/787f79fc internet_dating_mastery_pdf, https://pastebin.creamen.net/htdocs/view/51819aaf web_browser_voice_chat, https://paste.headstrong.de/view/deea28c2 badoo_da_profil_arama, http://paste.purplehat.org/view/8ead0e1b chat_girl_novel, https://code.secondversion.com/view/3694bdee sexin_kadna_faydalar, http://paste.ainsey11.com/view/1496e87b zene_traze_poznanstva_zadar, https://paste.morestina.net/view/d0123ab2 dating_sites_ukrainske_piger, https://paste.fusioned.net/view/11b0ae8b gode_dating_apps, https://fulscript.nu/view/2b66c99c milf_date_video, https://n.wuss.pw/view/997f4f1f dating_tips_for_30_year_old_woman, https://paste.morestina.net/view/04b95c42 chat_badoo_gratis_venezuela, http://text.starltd.net/view/8cab503b descargar_tinder_para_nokia_asha_311, https://paste.gajim.org/view/ba95c6a8 alektra_blue_&_daniel_hunter_site_my_first_sex_teacher, https://k.ryp.io/view/c73dfe70 free_live_stream_aik_hammarby, http://pb.stoleyour.com/view/1bba10b4 christian_online_dating_sites_in_nigeria, https://bin.simulakrum.org/view/9b61a2aa cougar_dating_houston, http://spit.grangerhub.com/view/3a029c14 deguisement_lolirock_iris, https://paste.severedlink.net/code/view/7f5cd9be matrimoniale_anglia_mea, https://n.wuss.pw/view/c4461406 audio_kontakt_mali_oglasi, https://bin.simulakrum.org/view/28e4f4fd sex_bio_dansk_billeder, https://paste.feed-the-beast.com/view/96a14555 lev_elsk_spis,


So you can see the contents of the file above.


Write something to this Textarea to Write to the File Shown Above








So above is an area where you can write contents. When you click the submit button, the contents that you wrote will be written to the file, sample.txt.

If you wrote something and submitted it by clicking the submit button, you should see the contents above that show the file change to include this. You will have to refresh the page though after you submit the form to see the changes to the content of the file.

So you've now written contents to a file using PHP without having to manually go to the file and write it.

So how is this done in PHP code?

This is shown below.


PHP Code

The code to write contents to a file using PHP is shown below.



Or if you just wanted to use a single line to write contents to a file, you could use the line shown below.



So the code is very straightforward and simple.

All you have to do to write contents to a file is to use the file_put_contents() function. The first parameter in this function specifies the pathway and the filename. And the second parameter specifies the string that you want to write to the file.

Since we are writing the contents of our file to the current directory (right now this article is published in the Articles directory of this website), this file named sample.txt is saved in the sample.txt file in the Articles directory. However, if we were writing to the subdirectory named 'files', for instance, inside the Articles directory, the first parameter would be 'files/sample.txt'.

The second parameter is simply the string that you want to write.

Be aware, though, that the file_put_contents() function in PHP erases everything currently in the file and overwrites it with the content that you specify. Therefore, be careful. If you don't want to erase all the contents that you have in a file, this is not the function to use. It totally erases everything and replaces just with the text that you write to it. Therefore, make sure this is what you want when using this PHP function. You can see this above when using the textarea form.

Anyways, this just shows you briefly how to write contents to a file using the PHP file_put_contents() function.


Related Resources

How to Read the Contents of a File Using PHP

How to Copy a File Using PHP

How to Rename a File Using PHP

How to Delete a File Using PHP

How to List All Files in a Directory using PHP





HTML Comment Box is loading comments...