Check included file exists

This example basically checks whether the include file exists and is readable. A useful little check to have.


$file = ‘includes/includedfile.php’;
if(file_exists($file) && is_readable($file))


Write to a file

$text=(“Test text!”);
echo (“File successfully written”);


Limit file download speed

//Author: Jonas John
// local file that should be send to the client
$local_file = ‘’;
// filename that the user gets as default
$download_file = ‘’;
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file)) {
    // send headers
    header(‘Cache-control: private’);
    header(‘Content-Type: application/octet-stream’);
    header(‘Content-Length: ‘.filesize($local_file));
    header(‘Content-Disposition: filename=’.$download_file);
    // flush content
    // open file stream
    $file = fopen($local_file, "r");
    while (!feof($file)) {
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));
        // flush the content to the browser
        // sleep one second
    // close file stream
else {
    die(‘Error: The file ‘.$local_file.’ does not exist!’);


Replace a space with an underscore

Handy for renaming files and so on


$filename = str_replace(‘ ‘,’_’,$filename);



Count Lines in a Text File

This example counts the amount of lines in a text file, in this case we count the lines in the php.ini file on my system

//note the file below is for a windows machine
$testfile = “C:\\xampp\\apache\\bin\\php.ini”;
if (file_exists ($testfile))
$rows = file ($testfile);
echo count ($rows) . ” lines in this file”;
echo “Error, cannot open the file”;


Random image example

This is a simple random image script which is ideal for displaying basic images from a text file . In future examples we will expand on this and build some simple banner rotation systems.

The first step that you need to do is to create your file for storing your images and then insert the names of the images.

In the example here I called the file images1.txt .

Each one of the entries is on a seperate line and in this case because I stored the files in a sub-directory I inserted that also . the structure of the file was like this


and so on. Now we get to the script that will display a random image and again this is straight forward enough.

#random images example
#this is your file
$file = “images1.txt”;
#open the file
$fp = file($file);
#generate a random number
#get one of the entries in the file
$random_image = $fp[array_rand($fp)];
#display the entry
echo “<img src=’$random_image’></img>”;

Nothing ground breaking here , we open a file , we then generate a random number, we then get a random entry from the file and store this in the variable $random_image and then we output this as some HTML.

Note that in this example we have saved this as a seperate file and included it on the page . If the file was called random.php then we put the following code where we want the image to appear

<?php include(“random.php”); ?>