Adsense
Popular Posts
- MySQL workbench -"Could not decrypt password cache"
- Transfer modules between sites
- Install APXS in Redhat Linux
- super(props) in React
- Set Windows path command line
- MySQL date created and date modified
- JavaScript, remove trailing insignificant zeros after toFixed function
- React: connect(mapStateToProps, mapDispatchToProps)
- MySQLcase When Then END in comparison
- PHP function for input sanitizing
Sunday, December 29, 2013
PHP, file and directory operation
fopen, fwrite, fread, fcolse, file_put_contents, file_get_contents,
change file line break to HTML line break: nl2br
example:
if($handle=fopen(test.txt','r')){
$content=fread($handle);
fclose($handle);
}
echo nl2br($content);
Get line by line fgets
if($handle=fopen(test.txt','r')){
while(1feof($handle)(
$content=fgets($handle);
echo $content."<br />";
}
fclose($handle);
}
filesize: file size
filemtime: modified time (change content)
filectime: changed time (change content or metadata such as chmod)
fileatime: last access time such as read or change.
Example
echo strftime('%m/%d/%Y %H%M', filemtime($filename))."<br />";
touch($filename): update file time.
Directory operation
get current working directory: getcwd
create a new directory: mkdir
Example
makdir('mydir','0777'); //note permission may be overwritten by umask
makdir('mydir/dir1/dir2','0777',true);recursively create new directoy
change directory chdir
remove a directory: rmdir
is_dir: to check if it is a directory
opendir: open directory to get directory handler
readdir: to get each file in a directory
Example:
if($mydir=opendir('test')){
while($filename=readdir($mydir))
echo $filename.'<br />';
}
Close directory: closedir
scandir: read all file names in the directory into an array
Example:
$file_array=scandir('test');
foreach($file_array as $filename)
echo $filename.'<br />';
To remove . and .. in the array, we can add
if(stripos($filename, '.')>0) // i.e first occurrence of '.' not the first position.
>> »
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment