Following example shows you some useful functions in filesystem.
|
How to get the total size of a system disk drive in PHP?
$totalDiskSize= disk_total_space("C:");
echo $totalDiskSize.' Bytes';
echo ($totalDiskSize/1024).' Kb';
echo ($totalDiskSize/(1024*1024)).' MB';
echo ($totalDiskSize/(1024*1024*1024)).' GB'; |
How to get the free space of a drive in PHP?
$free_space=disk_free_space("C:");
$freeDsikSpace=diskfreespace("C:");
echo $freeDsikSpace.' Bytes';
echo ($freeDsikSpace/1024).' Kb';
echo ($freeDsikSpace/(1024*1024)).' MB';
echo ($freeDsikSpace/(1024*1024*1024)).' GB'; |
How to determine whether a file or directory exists using PHP?
|
$fileName="C:\autorun.inf";
if(file_exists($fileName)){
echo 'File exists';
}else{
echo 'File not exists';
} |
|
$folder_name="C:\\Program Files";
if(file_exists($fileName)){
echo 'Folder exists';
}else{
echo 'Folder does not exists';
} |
|
|
Get information about a file in PHP .
1. |
How to get a size of a file? |
|
$fileName='C:\\boot.ini';
if(file_exists($fileName)){
echo 'File size = '.filesize($fileName).' bytes';
}else{
echo 'File does not exists';
} |
2. |
How to get last modified time of a file? |
|
$fileName='C:\\IO.SYS';
if(file_exists($fileName)){
echo 'Last modified time '.date('d/m/Y H:i:s',filemtime ($fileName));
}else{
echo 'File does not exists';
} |
3. |
How to get last accessed time of a file? |
|
$fileName='C:\\boot.ini';
if(file_exists($fileName)){
echo 'Last accessed time '.date('d/m/Y H:i:s',fileatime ($fileName));
}else{
echo 'File does not exists';
} |
4. |
How to delete a file in PHP? |
|
$fileName='C:\\jkl.jpg';
if(file_exists($fileName)){
if(unlink($fileName)){
echo $fileName.' has beed deleted';
}else{
echo 'Delete faild';
}
}else{
echo 'File does not exists';
} |
|
|
|
|
|
How to rename a file or directory in PHP?
$old_file_name='C:\\abc.jpg';
$new_file_name='C:\\jkl.jpg';
if(file_exists($old_file_name)){
if(rename($old_file_name,$new_file_name)){
echo $old_file_name.' has beed renamed to '.$new_file_name;
}else{
echo 'Rename faild';
}
}else{
echo 'File does not exists';
} |
How to copy a file in to another location in PHP?
$old_file_name='H:\projects\hexa\data\old.txt';
$new_file_name='H:\projects\hexa\data\new.txt';
if(file_exists($old_file_name)){
if(!file_exists($new_file_name)){
if(copy($old_file_name,$new_file_name)){
echo $old_file_name.' has beed copied to '.$new_file_name;
}else{
echo 'Faild to Copy';
}
}else{
echo 'Unable to copy, a file with the same name is already exists';
}
}else{
echo 'Source file does not exists';
} |
How to delete a folder in PHP?
$folder_name='H:\projects\hexa\data';
if(file_exists($folder_name)){
if(unlink($folder_name)){
echo $folder_name.' has beed deleted';
}else{
echo 'Delete faild';
}
}else{
echo 'Folder does not exists';
} |
|
$folder_name='H:\projects\hexa\data2';
if(file_exists($folder_name)){
if(rmdir($folder_name)){
echo $folder_name.' has beed deleted';
}else{
echo 'Delete faild';
}
}else{
echo 'Folder does not exists';
} |
|
How to create a folder/directory in PHP
$folder_name='H:\projects\hexa\data';
if(!file_exists($folder_name)){
if(mkdir($folder_name,0777)){
echo $folder_name.' has beed created';
}else{
echo 'Unable to create folder';
}
}else{
echo 'Folder already exists';
} |
How to create a simple text file in PHP
$fileName='H:\projects\hexa\data\test.txt';
if(!file_exists($fileName)){
$file = fopen($fileName, 'w');
fwrite($file, 'This is a simple text file');
fclose($file);
}else{
echo 'File already exists';
}
$file_name='H:\projects\hexa\data\test.txt';
if(file_exists($file_name)){
$new_content = "This is a new line\n";
file_put_contents($file_name, $new_content);
/*create a new file or overight the contents
of existing file. You can use
FILE_APPEND flag to append new content to a existing file.*/
//file_put_contents($file_name, $new_content, FILE_APPEND);
}else{
echo 'File does not exists';
} |
How to read a file in PHP
$fileName='H:\projects\hexa\data\test.txt';
if(file_exists($fileName)){
$handle = fopen($fileName, "r");
$contents = fread($handle, filesize($fileName));
fclose($handle);
echo $contents;
}else{
echo 'File does not exists';
}
$fileName='H:\projects\hexa\data\test.txt';
if(file_exists($fileName)){
$contents = file_get_contents ($fileName);
echo $contents;
}else{
echo 'File does not exists';
}
|
0 comments:
Post a Comment