Jump to content


Photo

Shere my file lerning script.


  • Please log in to reply
3 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 August 2006 - 06:09 AM

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.

<?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 fopen
fclose($e);

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 August 2006 - 06:38 AM

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.

a
a++
r
r++
w
w++

fopen
fclose
file_exists
unlink()
is_dir
rename
mkdir
feof
fgets


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 09 August 2006 - 07:39 AM

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.

#4 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 09 August 2006 - 08:41 AM

file_put_contents, same as fopening, fwriting and then fclosing.
file_put_contents("filename.txt", "data");

It requires php5
I'm just a guy who likes to code for fun...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users