prosoxi.com | Archive | PHP

Read a file’s contents with PHP

PHP provides three built-in functions which allow you to easily read the contents of a file on your webserver. This is useful when, for example, another program may write information to the file and you could access that information through your script.

In this example, we’re going to use the following example 5 line file, saved as file.txt:

line 1
line 2
line 3
line 4
line 5

In the same directory as file.txt, we’re going to work with the PHP file test.php. We’ll outline the file reading functions below.

readfile()

readfile() is the least useful of the file functions. As a parameter, it takes a string which specifies the location of the file to read – this can be relative or absolute; since our file is the same directory as the script, we just need to pass the string ‘file.txt’ as this paramter. We call the function using the code readfile('file.txt') and it prints out the contents of the file straight to the browser:

// readfile() writes the contents of the file straight to the browser

echo 'Contents of file.txt using readfile():<br>';

readfile('file.txt');

The disadvantage of readfile() is that it doesn’t allow you to manipulate the file contents before displaying it – the next two functions we’re going to look at will allow you to do that.

file()

The code file('file.txt') will read the contents of file.txt into an array, which you can then manipulate in your scripts and display yourself. Each line of the file is stored in a seperate element of the array, this means that we can access and manipulate each line of the file seperately using normal array notation.

// file() loads the contents of file.txt into an array, $lines

// each line in the file becomes a seperate element of the array.

$lines = file('file.txt');

// now loop through the array to print the contents of the file

echo 'Contents of file.txt using file():<br>';

foreach ($lines as $line)

{

echo htmlspecialchars($line) . '<br>';

}

// we can also access each line of the file seperately

echo '3rd line of the file: "' . htmlspecialchars($lines[2]) . '"<br>';

In this example, we’ve loaded the contents of file.txt into an array called $lines then used foreach to loop through the array and display each line on the user’s browser. We then use $lines[2] to access the third line of the file (the element at array index 2, since line 1 is $lines[0]).

You may notice that we’ve used a function called htmlspecialchars() when displaying the file’s contents – this enables that special characters used in HTML, such as > and " are displayed correctly. This illustrates the power of file() over readfile() since readfile() was unable to perform this kind of processing.

get_file_contents()

The last method we will look at is get_file_contents(‘file.txt’) which is similar to file() however rather than returning an array, it returns a string with all the lines of the file. We can manipulate and display this in a similar way as with file():

// file_get_contents() reads the file and places the contents in a string

$fileString = file_get_contents('file.txt');

echo 'Contents of file.txt using file_get_contents():<br>';

echo nl2br( htmlspecialchars($fileString) );

Since we have no way of seperating the lines with this method, we’ve used the nl2br() function which converts line breaks in the string (represented by the special character n) into the HTML line break <br /> so that the file will display correctly on the visitor’s browser.

The full contents of test.php is below:

<?php

// file() loads the contents of file.txt into an array, $lines

// each line in the file becomes a seperate element of the array.

$lines = file('file.txt');

// now loop through the array to print the contents of the file

echo 'Contents of file.txt using file():

';

foreach ($lines as $line)

{

echo htmlspecialchars($line) . '<br>';

}

// we can also access each line of the file seperately

echo '3rd line of the file: "' . htmlspecialchars($lines[2]) . '"<br>';

echo '<br>';

// file_get_contents() reads the file and places the contents in a string

$fileString = file_get_contents('file.txt');

echo 'Contents of file.txt using file_get_contents():

';

echo nl2br( htmlspecialchars($fileString) );

echo '<br>';

// readfile() writes the contents of the file straight to the browser

echo 'Contents of file.txt using readfile():<br>';

readfile('file.txt');

?>

http://www.tiposaurus.co.uk/2011/04/05/reading-a-files-contents-with-php/

The output from test.php is:

Contents of file.txt using file():
line 1
line 2
line 3
line 4
line 5
3rd line of the file: "line 3 "

Contents of file.txt using file_get_contents():
line 1
line 2
line 3
line 4
line 5

Contents of file.txt using readfile():
line 1 line 2 line 3 line 4 line 5

Posted in PHP0 Comments

Spin Text For SEO – A PHP Spinner

I recently had an SEO expert give us a few hours of his time to provide some suggestions to compliment our SEO strategy. One of the techniques that he introduced I was so impressed with (due to it’s utter simplicity) that I am kicking myself for not thinking of it before! Basically, if you have ‘doorway’ pages into your site (e.g. you have pages for ‘Cambridge Widgets’ and ‘Preston Widgets’ and alike) and want them to be dynamically generated, from the same content, but not to suffer horrible duplicate content penalties, you can use a ‘spinning’ function to generate contextually similar, different, content. The original concept is here, but I have enhanced it a bit to better suit my needs.

Continue Reading

Posted in PHP0 Comments

PHP Walker 1.0

Developed by Arthur Borisow, listed in Algorithm

Walker can be used to convert an array into hierarchic data. It can traverse an array of elements which may be objects or arrays that have entries that define an identifier of the entry and the identifier of the parent entry.

Continue Reading

Posted in PHP0 Comments

PHP Walker Class

walker class. This class will load results by executing given SQL query and then create multidimensional array that will have entries in right position. Let’s say that you have this data in your database.

Continue Reading

Posted in PHP0 Comments

Simple Class API Generator

SCA Generator Source

Shown below is the source code for the API generator. As you can see, I am using the SCA_Generator class itself to generate its own API.

Continue Reading

Posted in PHP0 Comments

phpVirtualBox

An open source, AJAX implementation of the VirtualBox user interface written in PHP. As a modern web interface, it allows you to access and control remote VirtualBox instances. Much of its verbage and some of its code is based on the (inactive) vboxweb project. phpVirtualBox was designed to allow users to administer VirtualBox in a headless environment – mirroring the VirtualBox GUI through its web interface.

Continue Reading

Posted in Linux, PHP, Programming, Windows0 Comments

php MATRIX encrypion and decryption program

 

cdl_capture_2013-01-04-05_ 001

Continue Reading

Posted in PHP0 Comments

php Nested Set Trees

About Nested Set Trees

When I learned programming, I was intensively taught how to deal with data structures in programming languages like C, C++, Ada, Modula, … When I tried to represent hierachical structures (tree structures) with databases, I realized that different representations and algorithms are more appropriate. Continue Reading

Posted in PHP0 Comments

PHP exception handling

What is an Exception

With PHP 5 came a new object oriented way of dealing with errors.

Continue Reading

Posted in PHP0 Comments

Couchbase Server

Always Scalable. Always Fast. Always On.

Continue Reading

Posted in Linux, MAC, PHP, Windows0 Comments

install Memcached Server and access it with PHP

Thinking of implementing caching for your php application , you are at a right place. Just in 10 simple (copy and paste) steps you can install and access Memcached Server.

Continue Reading

Posted in PHP0 Comments

Download Windows Binaries APC

APC

Alternative PHP Cache (APC) project is free and open. It optimizes PHP code and caches data and code in the shared memory. The project home page is http://pecl.php.net/package/APC where you can download its source code. Binaries that you can download from this site are built from official releases of APC and PHP. The only added thing is the version info sheet for dlls.

Continue Reading

Posted in PHP, Windows0 Comments

How to determine the first and last iteration in a foreach loop?

If your array has unique array values, then determining the first and last element is trivial:

Continue Reading

Posted in PHP0 Comments

PHP: Crontab Class to Add, Edit and Remove Cron Jobs

Provided that your user account on the server has the privileges to access crontab thus can create or remove cron jobs, you can use this PHP class to integrate crontab in your application. I created it for many of my own projects that need crontab to do scheduled jobs. It’s pretty straightforward.

Continue Reading

Posted in PHP0 Comments

Benchmarking PHP GEttext

I created a simple web page to compare the performance of various localization methods for PHP. It only contains 3 localized strings and does not use advanced features of gettext (e.g. plurals). I wrote a version using the gettext PHP extension (“gettext Ext.”), one using PHP-gettext (“gettext PHP”, a gettext implementation written in pure PHP) and a version that does not use gettext at all, instead it uses an array that contains all the translations (“String ID”).

Continue Reading

Posted in PHP0 Comments

Netbeans setup wamp xdebug

First, check that it isn’t actually working for you, and you don’t notice. I’ve done this…convinced it’s not working I wasted a chunk of time trying to get it to work, only to find that everything was OK.

Continue Reading

Posted in PHP, Windows0 Comments

Whocallsme.gr Widget Lookup

Domain Name Finder, Lookup, Whois

Domain Finder Tool by Truste.gr

read more for domains

Advertise

Sponsors

Wedstar all about wedding - gamos - γαμος
Κατασκευή ιστοσελίδων
Domain Names | Hosting