18
Filing System

Filing system in PHP

Embed Size (px)

Citation preview

Page 1: Filing system in PHP

Filing System

Page 2: Filing system in PHP

WHAT IS FILE

• A collection of data or information.• One that has a name, called the filename. • Almost all information stored in a computer must be in a file. There are many different types of files:

data files text files program files directory files and so on.

Different types of files store different types of information. For example, program files store programs, whereas text files store text.

OR A file is an object on a computer that stores data, information, settings, or commands that are

used with a computer program. In a graphical user interface (GUI) such as Microsoft Windows, files are shown as unique icons that relate to the program that opens the file.

Page 3: Filing system in PHP

FILE SYSTEM : magic constants

• MAGIC CONSTANTS• __FILE__ // The full path and filename of the file• __LINE__ // The current line number of the file.• __DIR__ // The directory of the file. (only php 5.3)

• PHP FUNCTION• dirname(__FILE__); • File_exists(__FILE__);• File_exists(dirname(__FILE__) )• File_exists(dirname(__FILE__).”/basic.html”) ? “yes”:”No”• Is_file() ;• Is_file(dirname(__FILE__).”/basic.html” ) ? “y”:”n”• Is_dir();• Is_file(dirname(__FILE__) ) ? “y”:”n”

Page 4: Filing system in PHP

UNDERSTANDING FILE PERMISSIONS: windows file permissions

Page 5: Filing system in PHP

PHP AND FILE PERMISSIONS

• Octal notation: 764

Page 6: Filing system in PHP

PHP AND FILE PERMISSIONS

• Chown()• Chown(‘file_name.php’,’owner_name’) ; • Chown only works in php is super user • Making webserver/PHP a super user is a big issue

• Chmod();• Fileperms(); • Fileperms(‘file_name.php’) // i.e 103306• Decoct();• Decoct(fileperms(‘file_name.php’)) • Sprintf()• Is_readable();• Is_writable();

Page 7: Filing system in PHP

ACCESSING FILES

• Php provides some functions for handling files.• We can create, write , read, append, copy and their permission can

be set/done.

• fopen — Opens file or URL• fwrite —• fread —• filesize — Gets file size• fgets — Gets line from file pointer

Page 8: Filing system in PHP

ACCESSING FILES

• ACCESSING FILES• fopen( filename , mode );

• Note: When writing to a text file, be sure to use the correct line-ending character! Unix systems use \n, Windows systems use \r\n, and Macintosh systems use \r as the line ending character.

Page 9: Filing system in PHP

ACCESSING FILES

Mode Purpose

r • Opens the file for reading only.• Places the file pointer at the beginning of the file.

r+ • Opens the file for reading and writing.• Places the file pointer at the beginning of the file.

w • Opens the file for writing only.• Places the file pointer at the beginning of the file.

and truncates the file to zero length. If files does notexist then it attempts to create a file.

w+ • Opens the file for reading and writing only.• Places the file pointer at the beginning of the file.

and truncates the file to zero length. If files does notexist then it attemts to create a file.

a • Opens the file for writing only.• Places the file pointer at the end of the file.• If files does not exist then it attemts to create a file.

a+ • Opens the file for reading and writing only.• Places the file pointer at the end of the file.• If files does not exist then it attemts to create a file.

x • Create and open for writing only; place the file pointer at the beginning of the file. • If the file already exists, the fopen() call will fail by returning FALSE If the file does not exist, attempt to create it.

X+ • Create and open for reading & writing only; place the file pointer at the beginning of the file. • If the file already exists, the fopen() call will fail by returning FALSE If the file does not exist, attempt to create it.

Page 10: Filing system in PHP

$file = ‘file-name.txt’;$handle = fopen( $file , ‘w’)

fclose( $handle );

If ( $handle = fopen( $file , ‘w’)){flose($handle );

}

ACCESSING FILES

Page 11: Filing system in PHP

$file = ‘file-name.txt’;If($handle = fopen( $file , ‘w’) ) { overwrite

Fwrite( $file , “abc”);Fwrite( $file , ‘123’) ;

Fwrite ( $file , ‘abc\n123’) ;

}

WRITING TO FILES

Page 12: Filing system in PHP

• Once a file is opened using fopen() function it can be read with a function called fread(). • This function requires two arguments:

• The file pointer • The length of the file expressed in bytes.

• The file's length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.• So here are the steps required to read a file with PHP.

• Open a file using fopen() function.• Get the file's length using filesize() function.• Read the file's content using fread() function.• Close the file with fclose() function.

READING FILES

Page 13: Filing system in PHP

if( $handle = fopen( $file , 'r') ){//$filesize = filesize( $file );//$contents = fread( $handle ,$filesize );

//echo fgets( $handle);

while(!feof( $handle )){$contents .= fgets( $handle );

}}echo $contents ; echo nl2br($contents);

READING FILES

Page 14: Filing system in PHP

$file = ‘filetext.txt’;If($handle = fopen( $file , ‘w’)){

Fwrite($handle , “123\n456\n789”);

$pos = ftell ($handle);Fwrite( $handle , ‘abcdef’);

Rewind($handle );Fwrite( $handle , ‘xyz’);

Fclose($handle);

// beware, it will overtype!!!//Note : a and a+ modes will not let you move the pointer

}

MOVING THE FILE POINTER

Page 15: Filing system in PHP

• File_put_conents : shortcut for fopen / fwrite / fclose

$file = ‘filetest.txt’;$content = “111\n222\n333”;

if( $size = file_put_contents( $file , $conent )){Echo “a file of {$sie} bytes was created ”;

}

WRITING TO FILES

Page 16: Filing system in PHP

• Close files first • Cant delete open files

• Must have permission on folder containing the file

Unlink(‘file_name’);

DELETING FILES

Page 17: Filing system in PHP

• getcwd — Gets the current working directory.• rmdir — Removes directory.• mkdir — Makes directory.• chdir — Change directory.• rename — Renames a file or directory.• readdir — Read entry from directory handle.• opendir — Open directory handle.• pathinfo — Returns information about a file path.

WORKING WITH DIRECTORIES

Page 18: Filing system in PHP

• readfile — Reads the contents of a file and returns the number of bytes read on success

Other PHP File System Functions