PHP- Working with directories



working with files
//1. Create or open file
//2.Read or write file
//3. Close file


//1. Create or open file


$fp=fopen(“abc.txt”,”r”);


//2.Read or write file
fgetc(), fgets(),fread(),file()


fwrite(),


//3. Close file
fclose(“abc.txt”);


Reading


fgetc($fp)

$fp=fopen("abc.txt",'r');

while($c=fgetc($fp)){
echo $c;
}
fclose($fp);


fgets($fp,1024)


fread($fp,filesize(“abc.txt”)/2);


Writing
fwrite($fp,”write this into file”);//fopen must be in write mode


unlink();//to delete filetouch()//to create file


rename(   , )copy(  , )
fseek($fp,0/filesize(“”)/2), filesize(“/abc.tx”),basename(),file_exists();
is_readable(),   is_file()

Locking file::flock($fp,1/2/3);1:read
2:no read no write
3. release lock


Reading file without opening

<?php
// Dump the games.csv file into an array
$games = file('games.csv');
// Parse the array
foreach($games as $game) {
echo "{$game}<br />";
}
?>







Working with Directories


$handle=opendir(‘/path/abc’’)


while($fname=readdir($handle))


mkdir(“name_of_folder”)


rmdir()


closedir($handle);

Reactions: