sobota 12. října 2019

PHP: file_get_contents/file_put_contents you can lose data!

As I have written in this article:

And prooved buffering by this tests on remote server:
Do not use this functions when you modify the file you are working with. Either use fflock, fread, fflush, ffwrite work-around or you can lose your data when you use file_get_contents/file_put_contents.

The reason why there is no mention about it on internet is that most programmers of website use databases for purposes of data modifications (instead of files).

Another interesting article about atomicity is at Czech (also read slovak comments). There are some interesting solution. This is also about non-atomic file solutions. But there is also mention that the file can be opened between fopen and flock, which explains why in my stests T5-T7 there were these problems. Read the article at English or translate it. Mr. Vrana also has a cool solution of page caching.

Read this:

There's the problem of calling fopen and then flock. Between these two operations (at least I think), another instance of the script can enter and open that file (for reading). If the original fopen was opened for writing, you probably lose the data in that file.
Read Jakub Vrána:

Probability increases with the number of processes working on the same task, which I write in the article. This is unrelated to PHP's intelligence - the operating system determines which process is going to be the turn - at any time it can remember that it will give the opportunity to another process.

Žádné komentáře:

Okomentovat

Python: Rozdíl mezi přidáním a rozšířením pole - append vs extend'!

  Výborná otázka – pojďme si vysvětlit rozdíl mezi append() a extend() a proč je v tomto konkrétním případě správné použít extend() . ...

Štítky

.profile adm administrace Adobe AI akcelerace alfa transparence analýza AND any aplikace apt ar archiv asociativní pole atomicity audio autentifikace awk balíčkovací systém bash beacon beacon_hint benchmark Bézierovy křivky bezpečnost biblehub BJT blogger boolean buffer buffering Cache-Conrol Cloudflare code Collector Cut-off ColorManager colorpicker common compare config cookies CPU CPU pipe css CSS3 curl current code cut čas data loss data lost data transfer reliability datasheet datetime.strptime deb deb-systemd-helper debian debián depricated development dioda diody dpkg dpkg -S dpkg-deb drivers EBO Emitter Cut-off Current eps ETag exclude exec Expires extrakce jediného extrakce názvu balíčku souboru extrakce obrázků extrakce souboru .deb fflock fflush ffmpeg FIFO file read file write file_get_contents file_get_contents/file_put_contents file_put_contents filter find first_install.sh flock Fly-back dioda font-face fóra fotorezistor fread functions funkce fwrite gate gate drive GDVfs gedit gedit-common geolokace getdata Ghostscript GIO glib gnome gnome settings GNU Privacy Guard gnupg gpg gradient-background grafika grep grep -v groupadd grub grub update gs gsettings gtk gtk.css gtk+ hebrejština history hlavičky HS html html 5 https hudba charakterizace chatGPT chroot chyba ICES IGBT Image img sizes img srcset impedance implementace imshow inference inkscape inrush current install jalový výkon javascript javescript jednocení seznamů js jsonData kapacita součástek koeficient zesílení komponenty xFce komunikace se serverem koncept konfigurace kontejner korekce barev Krita KSF Last-Modified lazy caching led LEFT JOIN librosa ligatury light-locker lightdm linux list log maják manuál maskování maskování služby masky matplotlib Max-Age measure memory měření MFCC MFCC koeficienty mint Mint 21.3 Mint xFce míry modules moralizace morphologie MOSFET mount moviepy mysql náběhový proud napěťová ochrana nastavení šablony návod nel Network Error Logging NLP normalizace šedi po resize not Notifications NTFS nth-child oblasti oblékání ochrana okruhy přátel OpenVINO IR formát oprava oprava balíčku optočlen org.gnome.desktop.screensaver org.gnome.nm-applet ořezové masky OSHB otázky otázky_jazyky otázky_moralismu_řešení ovladače panely parsování path pdf personifikace photorec php php 4 php 5 php 6 php 7 php 8 phpbb phpBB3 PipeWire pitch PN přechody pnp pole Policykit postscript práva profilování program prune průraz přeinstalování přepěťová ochrana přepolování příkazy připojení k síti připojení k wifi pseudokódd pstoedit PulseAudio PWM regulátory pydub python python3 pytorch ramdisk RBE RDSon read reaktance rectifier regulace vstupního napětí reinstall relyability remount replace restore reverzní geolokace RIGHT JOIN rm role rozvržení disků pro OS linux a data databází řešení samba scan scroll sdílení sdílení souborů Sec-Fetch-Dest Sec-Fetch-Mode Sec-Fetch-Site Sec-Fetch-User Secure Shell sed Set Cookie show-manual-login show-remote-login shunt schemas schémata schottka skript skupiny sloupce služby small song sort soubory soundfile spínané zdroje spínání splines split správa diskových zařízení SQL ssh stabilizace napětí stahování stíny stream string strojové učení stropové učení supplicant svg syntax systemctl systemd-logind T5 tabulka tabulky Tangentové úsečky tar témata tepelná ztráta terminologie test text-shadow themes thermal runaway time timestamp tkinter tr transistor transition tranzistor tranzistory tuple tvorba otázek TVS ubuntu účiník udiskd udisks unconfined underrun unity-greeter update usermod uživatelé va charakteristika vala vektorová grafika Vgs video Vth vynechání adresářů vývoj while wpa wpa_supplicant wrapovací funkce x xandr xapp-watt xargs -I xed xed-common xfdesktop xml XOR Xorg Xorg Thumbnails xrandr závislosti zdánlivý výkon zdroj zenerka zenerovo napětí zip zip archiv zkratky zpomalení zpracování textu zrychlení Žalmy