|
|
|
Due to stateless nature of the web Files are one of the most important feature of
any scripting language. In this tutorial learn how files are managed in PHP.
In this tutorial we will learn different features of File Access Method. Please
create a "hi.txt" file in the directory where you are testing your php scipts and
put some text in it.
Opening a File
fopen()
=======
<?
$fp=@fopen("hi.txt","r") or die("Could not open file.);
?>
Die is same as exit(), i ends PHP scripts execution and display message. The fopen()
function opens a pointer to the file on the filesystem.
"r" is for read access.
[r] read only xss ,
pointer beginning[r+] r & w xss,
pointer beginning[w] w xss,
pointer beginning, file exists-delete its contents , if does not exist create a new
one.
[w+] r & w xss, pointer beginning, file exists-delete its contents , if does not
exist create a new one.
[a] w xss, pointer at the end of file, if file doesnt exists create it.
fgets
=====
fgets -- Gets line from file pointerstring fgets (int fp [, int length])Returns a
string of up to length - 1 bytes read from the file pointed to by fp. Reading ends
when length - 1 bytes have been read, on a newline (which is included in the return
value), or on EOF (whichever comes first). If no length is specified, the length
defaults to 1k, or 1024 bytes.
fgets will return a string of mentioned byte length default is 1024. Store this in a
string and loop through the file and print the line
<?
while($line=@fgets($fp,1024)){
print $line;
}
?>
Close the file with @flose($fp)
===============================
You can also use the file function to open a file and return an array of lines [EX-
2] $lines=file("hi.txt") and use foreach loop to read the lines.
Testing whether a file exists Use the file_exists() function which return true(1) if
the file exists and false(1) if the file doesnt exists.
<?
if(file_exists(File name)){
Echo "File Exists";
}else{
Echo"File doesnt exists.";
}
?>
Copying entire file to memory
=============================
use fread function with filesize()Point a file pointer to file and store it in a
string using
fread.$fpn="hi.txt";$fp=@fopen("hi.txt","rb");$file_cont=fread($fp,filesize($fpn));
Since
fread function returns string so you can use print or echo to see the result
Creating a Temp File
====================
Use tempnam() function which generated a temporary file name in a specified
directory.
When you create a temp file make sure to delete it with unlink
commnad$tmpf=tempnam("c:\","tmpfile");Now open it as you did in previous examples
when you are done use @unlink($tmpf);If the directory does not exist, tempnam() may
generate a file in the system's temporary directory, and return the name of that.
tmpfile
=======
Creates a temporary file with an unique name in write mode, returning a file handle
similar to the one returned by fopen(). The file is automatically removed when
closed (using fclose()), or when the script ends.
$temp = tmpfile();
fwrite($temp, "writing to tempfile");
fclose($temp); // this removes the file
Flushing the Cache If you use functions like file_exists, is_readable, filesize()
etc they are cached by the server. Use clearstatcache() function to clear the cache.
Locking Files
=============
File locking is to make sure that, no other programs or processes are accessing the
same file.Use PHP's flock function.PHP supports a portable way of locking complete
files in an advisory way (which means all accessing programs have to use the same
way of locking or it will not work).flock() operates on fp which must be an open
file pointer.
operation is one of the following values: * To acquire a shared lock (reader), set
operation to LOCK_SH
* To acquire an exclusive lock (writer), set operation to LOCK_EX
* To release a lock (shared or exclusive), set operation to LOCK_UN
* If you don't want flock() to block while locking, add LOCK_NBYou must tell your
program to die if flock() does not work, otherwise your program will hang until flock
()
locks.[chmod]int chmod (string filename, int mode)
Attempts to change the mode of the file specified by filename to that given in
mode.examplechmod($fn,0644) // rw-r--r-
No. of Lines in a file
======================
Use count in conjuction with file() for smaller projects for big projects use
pointer to the file with a while loop.file(filename), count(file(filename);
Processing every word in a file
===============================
Use preg_split in conjuction with implode and file.First get a the file into an
array using file() function- file($filename) implode it into a single string implode
("",file(filename)) and then split variable by space using preg_split. Then use
foreach loop to print every word in array.
Reading a file Backward
=======================
Get the line array using file function and then use array_reverse()
function.$file_cont=array_reverse(file("hi.txt"));use foreach loop to print the
result.
END |
|
| Random Image Display Categories : PHP, Filesystem, Graphics, HTML and PHP | | | PHP based Contact email form with multiple recipients, text file based, supports departments. Categories : PHP, Email, Beginner Guides, Filesystem | | | The toll booth Categories : PHP, Java Script, Filesystem | | | php jump urls...the best way Categories : PHP, URLs, Filesystem | | | Simple way to replace a variable value in a .conf (.ini) file using a
webbrowser - the first stage of a complete universal configuration editor Categories : PHP, Regexps, Code Editors, Filesystem | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | File Explorer, browse, upload, download and edit your web site files with only a browser and a HTTP connection. Categories : Complete Programs, Content Management, Filesystem, PHP | | | GuestBook Light - a plug and play application for any website. Categories : PHP, Complete Programs, Filesystem, Sessions | | | Remote File Saving with PHP - Download and serve a remote file. The content of the file will be updated at fixed intervals. Categories : PHP, Filesystem, Cache, Sockets, HTTP | | | JSON File Upload Categories : PHP, AJAX, Filesystem | | | Get the correct extension and MIME types of an image, even when the filename is incorrect. Categories : PHP, Filesystem, General SQL | | | Read DPI value from image with PHP Categories : PHP, Graphics, Filesystem | | | Simple pipe delimited file export program that downloads to a local machine Categories : PHP, Filesystem, Databases, MySQL, HTTP | | | Single-file PHP news system with automatic folder structure creation Categories : PHP, Filesystem, Arrays | | | Recursive function to move files on a filesystem. It can be minor changed in order to copy recursively.
Categories : PHP, Filesystem, Algorithms | |
|
|
|