redarrow Posted August 9, 2006 Share Posted August 9, 2006 I was learning the file usage in php here i wrote a basic code but was fun i thort i would shere.This script will make a directory then insert a file then rename the file then display it.[code]<?php// post all varables$user_id=$_POST['user_id'];$filename=$_POST['filename'];$filemode=$_POST['filemode'];$message=$_POST['message'];$username=$_POST['username'];//set all varables$username="redarrow.txt";$user_id="0002";$filename="test.txt";$filemode="a";$message="my name is redarrow and i love php!";// see if directory exist using is_dir.if(!is_dir($user_id)){// make a dir using mkdir.$a=mkdir($user_id);}// if the file is there using file_exists.if(file_exists($user_id)){// open the dir using fopen.$b=fopen("$user_id/$filename","$filemode");// write to that file using fwrite.$c=fwrite($b,$message);// close the fopen to do a rename.fclose($b);//rename the file using rename.$d=rename("$user_id/$filename","$user_id/$username");}// open the dir with fopen $e=fopen("0002/redarrow.txt","r")or die("sorry no such file");// loop trow the fopen dir filename.while(!feof($e)){// get all the information using fgets line by line.$dis=fgets($e);//echo the information to the screen.echo $dis;}//close e fopenfclose($e);?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 9, 2006 Author Share Posted August 9, 2006 I wont someone to show me some more things i can do with files and directorys please and examples would be grate.so far i have learn the following statements.aa++rr++ww++fopenfclosefile_existsunlink()is_dirrenamemkdirfeoffgets Quote Link to comment Share on other sites More sharing options...
extrovertive Posted August 9, 2006 Share Posted August 9, 2006 Well, when I was learning file processing, I made a basic Hangman program..- Create PHP page where a user enter a word and a category through a form.- Create a PHP page where a user can enter 5 words and a category for each through a form.(ex. Word: <input..>...Category<input...>)- If a directory for category on your folder does not exists, make that directory.(ex. word = "dog" and category = "animal" would be stored in /animal/words.txt) words.txt would contain "dog"If a directory does exist, then append that word to the file.(ex. word = "bear" and category = "animal"). words.txt would contain "dog" and "bear" on seperate lines in the /animal/words.text path- Each word they enter will be stored in the text-file in different directory depending on the category.- Once a user enter 5 words, the hangman program will start.- Select a directory(category) and a random word from your text-file.- Have the user guess a letter for that random word through a form.- A correct letter will show the single letter of the word.- An incorrect letter will start to "hang" the person.- If a user correctly guess all the letter, that word will be deleted from the text-file.If you can write that program, that's a good basic exercise for text processing and other basic PHP stuff. Quote Link to comment Share on other sites More sharing options...
Ifa Posted August 9, 2006 Share Posted August 9, 2006 file_put_contents, same as fopening, fwriting and then fclosing.[code=php:0]file_put_contents("filename.txt", "data");[/code]It requires php5 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.