Duminda Chamara

Thursday, November 25, 2010

php file system functions

PHP Filesystem Functions

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';
}






©-Copyright By Duminda Chamara    JavaScript Validation  

Monday, November 8, 2010

Change style property using JavaScript

How to change style properties using JavaScript ?

Following example shows you how to change style dynamically in JavaScript.
In some occasions you may want to change the appearance of html elements dynamically. You can use JavaScripts to do this. Using Javascript you can set these style attributes dynamically. The following example shows you how to change style properties using javascript.

This example shows you how to change style properties using JavaScript. You can test this by selecting style properties from Style Settings section. Your changes will appear here. Remember that some style properties may not work in some browsers. In such case please use another browser to test this example.

This example shows how to change,
  • Font Family
  • Font Size
  • Font Color
  • Text Decoration
  • Text Transformation
  • Background Color
  • Text Align
  • Letter Spacing
using Javascript.
Style Settings
Font Family :
Font Size
Font Color
Text Decoration
Text Transform
Background Color
Text Align
Letter Spacing


1. Change background color in JavaScript.

html code

<p id='text-to-change'>Place your text here</p>

Javascript Code

document.getElementById("text-to-change").style.background = '#FCFCFC';

2. Change font family using JavaScript.

html code

<p id='text-to-change'>Place your text here</p>

Javascript Code

document.getElementById("text-to-change").style.fontFamily = "Verdana, Arial, Helvetica, sans-serif";

3. Change font color in JavaScript.

html code

<p id='text-to-change'>Place your text here</p>

Javascript Code

document.getElementById("text-to-change").style.color = '#ADADAD';

4. Change font size using JavaScript.

html code

<p id='text-to-change'>Place your text here</p>

Javascript Code

document.getElementById("text-to-change").style.fontSize = '10px';

5. Change text alignment in JavaScript.

html code

<p id='text-to-change'>Place your text here</p>

Javascript Code

document.getElementById("text-to-change").style.textAlign='center';//left,right,justify

6. Change text case using JavaScript.

html code

<p id='text-to-change'>Place your text here</p>

Javascript Code

document.getElementById("text-to-change").style.textTransform='capitalize';//capitalize,lowercase,uppercase,none

7. Change text decoration in JavaScript.

html code

<p id='text-to-change'>Place your text here</p>

Javascript Code

document.getElementById("text-to-change").style.textDecoration='underline'; //overline, line-through, blink, none

8. Change letter spacing using JavaScript.

html code

<p id='text-to-change'>Place your text here</p>

Javascript Code

document.getElementById("text-to-change").style.letterSpacing='2';;

©-Copyright By Duminda Chamara JavaScript Validation