Store Front Contact Us Projects Blog PHP Tutorials Community Forums Support Downloads Client Area Homepage Charlie Page - Home to the blog of myself! Dynasty Wizard - Free game dynasties, period. php cms reviews - find the right cms for you. 24 Quotes - Jack Bauer, Tony Almeida - Season 6 Bengals Chatter - The unofficial blog of the Cincinnati Bengals, recaping game by game along with opinion articles and community for Jungle fans to chat away. Political Yard - Talk about politics!
Insane Visions
Insane Visions
Insane Visions - Navigation

Favorites

Get Firefox!

PHP

MySQL

Affiliates


AllConsoleGamers
TalkPHP
Script Dungeon
XBOX 360 News
Hilarious Videos
Free Myspace Stuff

[ Apply for Affiliation ]



Scripts.com

MaxTutorial.com - Best photoshop, flash and php tutorials


Your Link Here
Your Link Here
Your Link Here

Acceptance Mark
Insane Visions - Top Navigation


Insane Visions - Basic PHP File Handling Tutorial :: Premium PHP Scripts - AdaptCMS, AdaptBB, OneCMS Basic PHP File Handling at Mar 16, 08 - 9:54 pm

Post to Digg Post to Facebook Post to Furl Post to Netscape Post to Newsvine Post to Reddit Post to Simpy Post to Spurl Post to StumbleUpon

Views: 10819
Type: PHP
Experience Level: Medium

We've mostly focused on MySQL-type tutorials, but we'll show you in this tutorial the basics of file handling. This includes opening a directory and listing the files, opening a file and showing it's contents and opening, then modifying a file. Deleting a file too!

Reading a Directory

<?php
if ($handle = opendir("folder")) {
while (
false !== ($file = readdir($handle))) {
if (
$file != '.' and $file != '..') {
echo
"<­a href='.$file.'>".$file."<­/a><br />";
}
}
closedir($handle);
}
?>

Alright we have several things going on. First opendir simply opens a directory, replace folder with the name of the folder and if needed, the path too. The while loop then goes through file-by-file in the directory, the if statement excludes showing two non-files, you can exclude any files you wish too. After a simple echo displaying the filename, we then close the directory.

Showing a File

<?php
$file 
'/path/to/file.php';
$handle fopen($file'r');
$contents fread($handlefilesize($file));
echo 
$contents;
fclose($handle);
?>

There are several ways to open and then display the contents of a file, but this example is for local files. You will see the fopen takes the file URL and then type r (meaning it will read the file, nothing else) onto the fread function. fread then gets the contents of the file, including a PHP file. fclose closes the connection after displaying the contents of the file.

Writing to a File

<?php
$contents 
'hey this is just a test, bla bla bla';
$handle fopen('/path/to/file.php''w');
fwrite($handle$contents);
fclose($handle);
?>

Okay first of all, if you do set the path to a non-existant file, it will attempt to create the file. If successfull it will replace the contents of the file with what you set in that "$contents" variable.

Setting the path and using fopen with type w, lets us open the file to write. fwrite then takes the "$contents" variable, putting the contents into the file and then closing it with fclose.

Deleting a File

<?php
unlink
('/path/to/file.php');
?>

Not much explanation is needed, simply put in the path to the file you want to delete and wuvala.

Conclusion

We'll be doing more intermediate-expert tutorials mixed in with this basic tutorials in the future. Being able to open a directory, open and show a file, open and write a file as well as deleting a file are important PHP basics. Happy coding!

Download:

Rating:

Vote for Article:



 email

 website







Username:
Password:
Insane Visions - Login Register

AdaptCMS

OneCMS

Latest Tutorials

- PHP and Forms
- Basic PHP File Handling
- PHP Search


Latest Blogs

- AdaptCMS - v1.2 Features
- Creating your first Website
- AdaptCMS - RSS Importer


Poll of the Month

How do you code in PHP?
Standard, all the time
I don't know PHP
It depends on the script
OOP, the one and only!
Results


Latest Posts

- Launch Upcoming
- Is this adapt BB?
- AdaptCMS Pro v1.1 -
Released


Testimonials

'I tried various other Content Management Systems before settling on OneCMS. None came close to the ease of use and the amazing customer service! This was my first time dealing with a CMS so naturally I had many, many questions. Amazingly they were all answered the same day, most of the time my problems were fixed in minutes! OneCMS has many great features and many more planned for future releases and I personally can't wait! I'm glad I went with OneCMS for my site VideoGamesHardcore.com!'

- Hector Cortez, Founder/Editor-In-Chief, VideoGamesHardcore.com



Powered by AdaptCMS Pro
Insane Visions - Footer
Loans - Mortgages - United Specialties - Credit Cards