PHP File System

PHP File SystemWe will discuss the common filesystem functions available to you. First of all, in order to do anything with a file, we must first open it. To do so we will use the following line:

$filehandle = fopen('/path/to/file', 'w');

The 'w' is an argument that tells the system what you plan to do with the script. Here are the possible choices:

'r' = Open the file and place the file pointer at the beginning of the file. You will receive an error if the file does not exist.
'r+' = Open file for reading and writing, otherwise same as above.
'w' = Write to a file. If the file does not exist, it will attempt to create it. If it does exist it truncates the file and you overwrite it.
'w+' = Open the file for reading and writing. Otherwise same as above.
'a' = Open the file for writing. If the file does not exist, it will attempt to create it. If it does exist, it will add to the end (not overwrite).
'a+' = Open file for reading and writing. otherwise same as above.

Now that we have successfully created a variable to open the file, lets go ahead and read it.

$contents = fread($filehandle, filesize('/path/to/file'));

The fread function requires both the fopen statement and the size of the file to read. Luckily, we have the filesize() function to find that for us. Now lets attempt to add some new content to the file.

fwrite($filehandle, 'This is some new content blah blah');

Once again, the fwrite function also requires our fopen $filehandle to work correctly. Lastly, make sure you close the file to prevent corruption and/or data loss.

fclose($filehandle);

That’s it!


Related Articles

  • Counter in ASP
    A hit counter is an essential part of a site to know how many people are coming to site. Here is an easy way to make a counter. All you need is access to ASP and be able to give a write access to a directory...
  • Sometimes It's The Little Things That Count!
    Recently, I decided to switch my web hosting company. I had been with my last hosting company for almost two years but I was having continuing problems with their system. So I switched and I am up and running wi...
  • Uploading Files With PHP
    Upload an image or file to your server without using your FTP.
  • Password Protection
    Although there are many uses for a .htaccess file, by far the most popular, and probably most useful, is being able to reliably password protect directories on websites. Although JavaScript can also be used to do this, only .htaccess has total security, because someone must know the password to get ...
  • Introduction to .htaccess
    .htaccess is a Unix/Linux based file for Apache web servers that allow you to change access permissions and many other things for files on your web server.
    To make a .htaccess file, open up an html editor, preferably notepad, and enter your commands(each on a seperate line). When saving your file...
  • Easy Flat File Hit Counter
    Create a very simple hit pageview counter without any database! Use flat file scripting to make an easy counter!
  • Simple Counter & Redirecting & Browser Type
    This is a really 2 file way of making a unique hit counter.
    If you make a new file called counter.txt and just type the number 1 on the first line.
  • Domains: Your Friendly Address on the Internet
    Would you believe that there was a time that the internet didnt have any domain names? During those days the internet was very much like a telephone network. If you wanted to visit a site, you would have to type that site IP address. And without domain names, visiting other websites became a chore....
  • Banner Rotation and PHP Include
    With this script you can add a banner rotation to your site, the base code is:
  • Advanced Image Uploader
    This is an advanced image uploader...

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.