Search:

Categories



Check included file exists

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

 

<?php
$file = ‘includes/includedfile.php’;
if(file_exists($file) && is_readable($file))
{
include($file);
}
?>

Share

Write to a file

<?php
$text=(“Test text!”);
$write_to_file=fopen(“Textfile.txt”,”a”);
fwrite($write_to_file,$text);
fclose($write_to_file);
echo (“File successfully written”);
?>

Share

Limit file download speed

//Author: Jonas John
// local file that should be send to the client
$local_file = ‘test-file.zip’;
  
// filename that the user gets as default
$download_file = ‘your-download-name.zip’;
  
// 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
    flush();
  
    // 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
        flush();
  
        // sleep one second
        sleep(1);
    }
  
    // close file stream
    fclose($file);
  
}
else {
    die(‘Error: The file ‘.$local_file.’ does not exist!’);
}

Share

Replace a space with an underscore

Handy for renaming files and so on

<?php

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

?>

Share

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

<?php
//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”;
}
else
{
echo “Error, cannot open the file”;
}
?>

Share

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

image/banner1.gif
image/banner2.gif
image/banner3.gif

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

<?php
#random images example
#this is your file
$file = “images1.txt”;
#open the file
$fp = file($file);
#generate a random number
srand((double)microtime()*1000000);
#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”); ?>

Share