Affordable Web Hosting with Excellent Customer Support internet connection free month hosting

PHP Read File

Imagine we have a text file called vars.txt. Inside vars.txt we have tab delimited data which we want to access and read into variables.

First let's look at our vars.txt file. In it are 3 strings separated by tabs. You can't see them when you open the file in your browser. But they appear to 'the computer' as \t for tabs.

Now let's open vars.txt and read all the contents into one variable.


#get server path to webshare (more info)

$data = fread(fopen($file, "r"), filesize($file));

# use fread, open for reading, how much? full size of $file.

$data_split = split("\t", $data);

#break up the data into $data_split array, split where the tabs are

#assign the first item of $data_split to $fname, etc

$fname=$data_split[0]; $lname=$data_split[1]; $age=$data_split[2];

#now print for viewing:

echo ("Filepath was $file<br>\n"); echo ("Name is $fname $lname, age is $age");

What can you do with this? Basically this gets rid of your need for a database system like sql or my-sql for simple data storage needs. If you need to store information files for images, videos, people, etc. You can save it all in text files, and using this system, open them and use the information.