[PHP] Jak na úpravu textového souboru?
Mám seznam ip adres na síti v jednom txt souboru. Vždy když přibyde nová ip přez web formulář a jednoduchý php script na další řádek doplním ip. Problém nastane když chci jednu ip smazat. To pak musím ručně přez ftp. Nevíte jak by se dal ze souboru odstranit řádek pomocí php?
Re: [PHP] Jak na úpravu textového souboru?
NApř. si načti soubor po řádcích do pole (fce file(soubor) ), vytvoř si ještě jedno nové prázdné pole a projdi to plné pole a přidávej řádky do nového pole, pokud řádek neobsahuje (rtrim(radek)!=ip) hledanou ip adresu. Pak si soubor otevři pro zápis (fopen) a zapiš do něj (fwrite) obsah nového pole převedeného na string (implode('', nove_pole)).
tak nejak by to mohlo fungovat :)
ale nebylo by lepsi pouzit nejakou db?
Re: [PHP] Jak na úpravu textového souboru?
Alebo po riadkoch nacitat do pola, vyprazdnit subor.
Potom nastavit pointer v subore na zaciatok a zapisat tam to pole s podmienkou
for ($i = 0; $i < sizeof($pole); $i++)
if ($pole[$i] != $ipcka_ktoru_chcem_vyhodit) fwrite...
P.S.
A DB by bola fakt na toto lepsia...
Re: [PHP] Jak na úpravu textového souboru?
Kolko tam tych IP je? Preco je to v subore?
Re: [PHP] Jak na úpravu textového souboru?
No v souboru je to jako docasne nez bude nova db....coz nevim jak dlouho bude...v db to bylo ale ted je mimo provoz. Dik moc za napady :)
Pozn Pit - nemělo by tam byt $i <= sizeof($pole) ale nejsem si jist jestli se tak zapisuje mensi nebo rovno :oops:
Re: [PHP] Jak na úpravu textového souboru?
Citace:
Původně odeslal Gregy
No v souboru je to jako docasne nez bude nova db....coz nevim jak dlouho bude...v db to bylo ale ted je mimo provoz. Dik moc za napady :)
Pozn Pit - nemělo by tam byt $i <= sizeof($pole) ale nejsem si jist jestli se tak zapisuje mensi nebo rovno :oops:
No jelikoz je to indexovane od nuly, tak sizeof pole o peti prvcich vrati 5, coz jsou indexy 0,1,2,3,4.
Re: [PHP] Jak na úpravu textového souboru?
Citace:
Původně odeslal Gregy
Pozn Pit - nemělo by tam byt $i <= sizeof($pole) ale nejsem si jist jestli se tak zapisuje mensi nebo rovno :oops:
Nemalo... A preco, ti napisal ZeeZ.
Ak mas napr. 20 riadkov, tak sizeof($pole) bude 20... a ja idem v cykle od 0 do 19, co je 20x... predpokladam, ze pole naplnis tiez for-om alebo while-om so zvysovanim premennej od 0...
Ked to zatial nemas v DB ale v subore, tak si davaj pozor, aby sa nestalo, ze ti ho edituje naraz viac ludi.
Re: [PHP] Jak na úpravu textového souboru?
Naco to davat do pola?
Kód:
while (!feof($infile)) {
$line = fgets($infile);
if ($line !== $ip)
fwrite($outfile, $line."\n");
}
Ak to ma byt poriadne, tak to takto treba zapisat do docasneho suboru (vid. funkcia tempnam()), potom oba subory zavriet a premenovat docasny na trvaly (tym sa atomicky zmeni = nikdy nikto neuvidi napoly zapisany subor). Este treba vyriesit viacnasobne spustenie toho skriptu (nejake zamykanie).
Re: [PHP] Jak na úpravu textového souboru?
Díky moc....ale zamykání nepotřebuju...kdyžtak si to najdu. Myslim ale že je hrozně malá pravďepodobnost že by sme se dva trefili když sme jenom dva :D
Re: [PHP] Jak na úpravu textového souboru?
Citace:
Původně odeslal Gregy
Díky moc....ale zamykání nepotřebuju...kdyžtak si to najdu. Myslim ale že je hrozně malá pravďepodobnost že by sme se dva trefili když sme jenom dva :D
Je to ale prace, kdyz clovek nema databazi... a tu ja uz zase nemam. Nainstaloval jsem PHP Home, Apache bezi, mysql bezi, localhost, root, root a nemuzu se tam prihlasit, protoze to pise, ze nemam prava :-)
Re: [PHP] Jak na úpravu textového souboru?
Nainstaluj si WAMP To funguje bez problemu. Jinak se koukni do dokumentace, tam to urcite bude ;) Nebo taky nemusis mit odkomentovany prikaz extension=php_mysql.dll v konfiguracnim souboru PHP.ini. Mel by byt umisteny nekde v miste instalace PHP.
Re: [PHP] Jak na úpravu textového souboru?
Trochu OT: prepsal bych to do sqlite, nepotrebujes zadny DB server, ani nic instalovat. Na tyhle jednoduchosti imho idealni. Podpora primo v PHP.