What is a File?
In this quick web dev tutorial, you'll learn how to process large files in PHP, avoiding methods that won't work because of memory limitations.
I've been trying to get.HTML files to process like.PHP files using the.htaccess file, I've used the following, both of which don't work and trying to access the test page gives me an option to. In this quick web dev tutorial, you'll learn how to process large files in PHP, avoiding methods that won't work because of memory limitations. I am trying to process somewhat large (possibly up to 200M) JSON files. The structure of the file is basically an array of objects. So something along the lines of: [ {'property':'value', 'prop.
A file is simply a resource for storing information on a computer.
Files are usually used to store information such as;
- Configuration settings of a program
- Simple data such as contact names against the phone numbers.
- Images, Pictures, Photos, etc.
In this tutorial, you will learn-
PHP File Formats Support
PHP file functions support a wide range of file formats that include;
- File.txt
- File.log
- File.custom_extension i.e. file.xyz
- File.csv
- File.gif, file.jpg etc
- Files provide a permanent cost effective data storage solution for simple data compared to databases that require other software and skills to manage DBMS systems.
- You want to store simple data such as server logs for later retrieval and analysis
- You want to store program settings i.e. program.ini
PHP files Functions
PHP provides a convenient way of working with files via its rich collection of built in functions.
Operating systems such as Windows and MAC OS are not case sensitive while Linux or Unix operating systems are case sensitive.
Adopting a naming conversion such as lower case letters only for file naming is a good practice that ensures maximum cross platform compatibility.
Let’s now look at some of the most commonly used PHP file functions.
PHP File_exists Function
This function is used to determine whether a file exists or not.
- It comes in handy when we want to know if a file exists or not before processing it.
- You can also use this function when creating a new file and you want to ensure that the file does not already exist on the server.
The file_exist function has the following syntax.
HERE,
- “file_exists()” is the PHP function that returns true if the file exists and false if it does not exist.
- “$file_name” is the path and name of the file to be checked
The code below uses file_exists function to determine if the file my_settings.txt exists.
Save the above code in a file named file_function.php Assuming you saved the file in phptuts folder in htdocs, open the URL http://localhost/phptuts/file_function.php in your browser You will get the following results.
PHP Fopen Function
The fopen function is used to open files. It has the following syntax
HERE,
- “fopen” is the PHP open file function
- “$file_name” is the name of the file to be opened
- “$mode” is the mode in which the file should be opened, the table below shows the modes
Mode | Description |
---|---|
r |
|
r+ |
|
w |
|
w+ |
|
a |
|
a+ |
|
- “$use_include_path” is optional, default is false, if set to true, the function searches in the include path too.
- “$context” is optional, can be used to specify the context support.
PHP Fwrite Function
The fwrite function is used to write files.
It has the following syntax
HERE,
- “fwrite” is the PHP function for writing to files
- “$handle” is the file pointer resource
- “$string” is the data to be written in the file.
- “$length” is optional, can be used to specify the maximum file length.
PHP Fclose Function
Is is used to close a file in php which is already open
It has the following syntax.
HERE,
- “fclose” is the PHP function for closing an open file
- “$handle” is the file pointer resource.
Let’s now look at an example that creates my_settings.txt.
Free convert to pdf file software. We will use the following functions.
- Fopen
- Fwrite
- fclose
Open a file |
Closing a file |
Create File |
Testing the code
Open the URL http://localhost/phptuts/create_my_settings.php in your browser.
You will get the following page
.
Electronic File Processing
Note: if your disk is full or you do not have permission to write files, you will get an error message.
Switch back to the URL http://localhost/phptuts/file_function.php .
What results do you get?
PHP Fgets Function
The fgets function is used to read php files line by line. It has the following basic syntax. fgets($handle); HERE,
- “$fgets” is the PHP function for reading file lines
- “$handle” is the file pointer resource.
Let’s now look at an example that reads my_settings.txt file using the fopen and fgets functions.
The code below read_my_settings.php implements the above example.
HERE,
- “fopen” function returns the pointer to the file specified in the file path
- “die()” function is called if an error occurs. It displays a message and exists execution of the script
PHP Copy Function
The PHP copy function is used to copy files. It has the following basic syntax. copy($file,$copied_file); HERE,
- “$file” specifies the file path and name of the file to be copied.
- “copied_file” specified the path and name of the copied file
The code below illustrates the implementation
Deleting a file
The unlink function is used to delete the file. The code below illustrates the implementation.
PHP File_get_contents Function
The file_get_contents function is used to read the entire file contents.
The code below illustrates the implementation.
The difference between file_get_contents and fgets is that file_get_contents returns the file data as a string while fgets reads the file line by line.
Summary
- A file is a resource for storing data
- PHP has a rich collection of built in functions that simplify working with files.
- Common file functions include fopen, fclose, file_get_contents
- The table below shows a summary of the functions covered
Function | Description |
---|---|
File_exists | Used to determine if a file exists or not |
fopen | Used to open a file. Returns a pointer to the opened file |
fwrite | Used to write to files |
fclose | Used to open closed files |
fgets | Used to read a file line by line |
copy | Used to copy an existing file |
unlink | Used to delete an existing file |
file_get_contents | Used to return the contents of a file as a string |
Active9 months ago
I am trying to process somewhat large (possibly up to 200M) JSON files.The structure of the file is basically an array of objects.
So something along the lines of:
Each object has arbitrary properties and does not necessary share them with other objects in the array (as in, having the same).
I want to apply a processing on each object in the array and as the file is potentially huge, I cannot slurp the whole file content in memory, decoding the JSON and iterating over the PHP array.
So ideally I would like to read the file, fetch enough info for each object and process it.A SAX-type approach would be OK if there was a similar library available for JSON.
Any suggestion on how to deal with this problem best?
The Mighty Rubber DuckThe Mighty Rubber Duck2,25633 gold badges2222 silver badges2626 bronze badges
6 Answers
Laserjet 1200 driver windows 7 64 bit. I've written a streaming JSON pull parser pcrov/JsonReader for PHP 7 with an api based on XMLReader.
It differs significantly from event-based parsers in that instead of setting up callbacks and letting the parser do its thing, you call methods on the parser to move along or retrieve data as desired. Found your desired bits and want to stop parsing? Then stop parsing (and call
close()
because it's the nice thing to do.)(For a slightly longer overview of pull vs event-based parsers see XML reader models: SAX versus XML pull parser.)
Example 1:
Read each object as a whole from your JSON.
Output:
Objects get returned as stringly-keyed arrays due (in part) to edge cases where valid JSON would produce property names that are not allowed in PHP objects. Working around these conflicts isn't worthwhile as an anemic stdClass object brings no value over a simple array anyway.
Example 2:
Read each named element individually.
Output:
Example 3:
Read each property of a given name. Bonus: read from a string instead of a URI, plus get data from properties with duplicate names in the same object (which is allowed in JSON, how fun.)
Output:
How exactly to best read through your JSON depends on its structure and what you want to do with it. These examples should give you a place to start.
Also known as Samsung Galaxy Ace La Fleur, Samsung Galaxy Ace Hugo Boss Network GSM 850 / 900 / 1800 / 1900 HSDPA 900 / 2100 HSDPA 850 / 1900 - Samsung Galaxy Ace S5830L HSPA 7.2/0.384 Mbps Yes Yes Launch 2011, January.
user3942918user394291818.8k1010 gold badges4242 silver badges5959 bronze badges
I decided on working on an event based parser. It's not quite done yet and will edit the question with a link to my work when I roll out a satisfying version.
EDIT:
I finally worked out a version of the parser that I am satisfied with. It's available on GitHub:
There's probably room for some improvement and am welcoming feedback.
The Mighty Rubber DuckThe Mighty Rubber Duck2,25633 gold badges2222 silver badges2626 bronze badges
There exists something like this, but only for C++ and Java. Unless you can access one of these libraries from PHP, there's no implementation for this in PHP but
jonijonijson_read()
as far as I know. However, if the json is structured that simple, it's easy to just read the file until the next }
and then process the JSON received via json_read()
. But you should better do that buffered, like reading 10kb, split by }, if not found, read another 10k, and else process the found values. Then read the next block and so on.4,27911 gold badge2020 silver badges3838 bronze badges
This is a simple, streaming parser for processing large JSON documents. Use it for parsing very large JSON documents to avoid loading the entire thing into memory, which is how just about every other JSON parser for PHP works.
Aaron AverillAaron Averill
Open File In Php
Recently I made a library called JSON Machine, which efficiently parses unpredictably big JSON files. Usage is via simple
foreach
. I use it myself for my project.Example:
See https://github.com/halaxa/json-machine
Filip HalaxaFilip Halaxa
There is http://github.com/sfalvo/php-yajl/ I didn't use it myself.
Alex JasminAlex Jasmin34.4k55 gold badges6666 silver badges6161 bronze badges
protected by tchristSep 6 '12 at 12:30
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?