• No results found

stat(file path) : file function Return an array with file information

In document php - notes (Page 155-166)

IN PHP IN PERL

1) stat(file path) : file function Return an array with file information

2)is_file(file path) : checks wheather the file exist or not & Returns boolean. 3)file_get_contents(file Path) : Returns the file content as a string.

4)file_put_contents(fiel path,data) : writes the content to the file overwriting old content &

iffile does not exists, creates a new file & adds the content..This function is introduced in 5th version)

5)file(file path) : Returns the file content as an array with each line in the file at a particular

index position.

6)filectime(path) : creates the time of the file.

7)fileatime(file path) : last access time of the path file. 8)filemtime(file path) : last modified time of the file. 9)unlink(file path) : Removes the file from the location. 10)filesize(file path) : Returns the size of the file in bites.. 11)filetype(file Path) : Returns the type of the file..(dir or file)

fopen(file path,mode) : Opens the file in the specified mode &returns a file handler for handling the file.

fread(file handler, size) : Reads the file content from the current file handler position upto the

specified size.

$fp -> file Pointer ..The internal handler which is used for handling the file. fgetc(file handler) : Reads a single char in the string.

fgets(file handler) : Reads a single line from the file..

fwrite(file handler) : This function is used to write the content to the file.. ftell(file handler) : retruns the file handler position in the file

fseek(file handler) : Moves the file handler to the specified position in the file fclose(file handler) : Closes the File handler connection.

List of Modes :

r --> Read Mode r+ --> Read and Write. w --> Write Mode w+ --> Write & Read.

a --> Append (also writing but without overwriting files) a+ --> append & read.

x --> Creates a new file & write..

x+ --> Creat a new file for write and read.

rb,rb+,wb,wb+,ab,ab+,xb,xb+ are the modes to be used for binary files for above specified action...

fileput.php <?php $file = 'a.txt'; file_put_contents($file,str_repeat("MOd ",5)); create.php <?php

$file = 'c.txt'; $fp = fopen($file,"x"); fwrite($fp," Created"); fclose($fp); if(is_file($file)){ echo file_get_contents($file); } write.php <?php $file = 'b.txt'; $fp = fopen($file,"w"); fwrite($fp,"Modified"); fclose($fp); if(is_file($file)){ echo file_get_contents($file); } read.php <?php $file = 'test.txt'; $fp = fopen($file,"r");

echo "Pos : ",ftell($fp),'<br>'; echo fread($fp,3),'<br>';

echo "Pos : ",ftell($fp),'<br>'; echo fgetc($fp),'<br>';

echo "Pos : ",ftell($fp),'<br>'; echo fgets($fp),'<br>';

echo fgets($fp),'<br>'; echo 'Pos : ',ftell($fp),'<Br>'; echo fgets($fp),'<br>'; fseek($fp,35); echo fgetss($fp),'<br>'; fclose($fp); func.php <?php $file = 'test.txt'; var_dump(is_file($file)); echo '<br>';

echo "size : ",filesize($file),'<br>'; echo "Type : ",filetype($file),'<br>';

echo "Created : ",date("d-m-y h:i:s",filectime($file)),'<br>'; echo "Accessed : ",date("d-m-y h:i:s",fileatime($file)),'<br>'; echo "Modified : ",date("d-m-y h:i:s",filemtime($file)),'<br>'; echo file_get_contents($file),'<br>';

print_r(file($file)); echo '<br>'; print_r(stat($file)); append.php <?php $file = 'b.txt'; $fp = fopen($file,"a"); fwrite($fp,"Append"); fclose($fp); if(is_file($file)){ echo file_get_contents($file); } unlink.php <?php $file = 'a.txt'; unlink($file); Directory files: -

getcwd() : current working directory,

chdir(path) : change current working dir to argumented path.

is_dir(path) : checks wheather the argumented dir exist or not and return boolean. mkdir(dirname) : makes a directory on the current working location.

rmdir(dir name) : removes the directory.

scandir(path) : scan the dir & return the directory content as an array .. Returns an array with

path related information.

getcwd.php <?php echo 'Cwd : ',getcwd(),'<br>'; $a = scandir('.',0); foreach($a as $k=>$v){ echo "$k == $v<br>"; } chdir('..');echo '<hr>'; echo 'Cwd : ',getcwd(),'<br><hr>'; $a = scandir('.',0); foreach($a as $k=>$v){ echo "$k == $v<br>"; } mkdir.php <?php $dir = 'test'; if(!is_dir($dir)){ mkdir($dir);

echo "Directory is created"; }

else {

echo "Directory already exists"; } rename.php <?php $dir = 'test'; $new = "modified"; if(is_dir($dir)){ rename($dir,$new);

echo "Directory name is $new"; }

else {

echo "Directory does not exists"; } rmdir.php <?php $new = "modified"; if(is_dir($new)){ rmdir($new);

echo "Directory is removed"; }

echo "Directory does not exists"; } scandir.php <?php echo '<pre>'; var_dump(scandir('.')); Super globals: -

Difference between GET and POST:-

GET POST

1.GET data transfers through URL. POST data is send through request headers

2.GET is insecure POST is secure

3.File cannot be transfered using GET Files can be transfered

4.Limited data can be send based on length We can send huge data(8MB) which can be scaled

of URL supported by browser(2KB). up by using POST_MAX_SIZE

5.It is fast It is not as fast as GET.

6.$_GET is used for accessing the GET parameters $_POST is used for accessing the POST

Get ex: -get.php

<?php

if(isset($_GET['submit'])){

echo 'Name : ',$_GET['fname'],'<br>';

echo 'Email Address : ',$_GET['email'],'<br>'; $gend = ($_GET['gender'] == 'm')?'Male':'Female';

echo 'Gender : ',$gend,'<br>'; }

echo '<hr>';

echo urldecode($_SERVER['QUERY_STRING']);echo '<hr><br>';

get.html

<form method='GET' action='get.php'>

Name : <input type='text' name='fname' value=''><br> E-Mail : <input type='text' name='email' value=''><br>

Gender : <input type='radio' name='gender' value='m'> Male <input type='radio' name='gender' value='f'> Female<br>

<input type='submit' name='submit' value='Register !'> </form>

Post ex: -post.php

<?php

if(isset($_POST['submit'])){

echo 'Name : ',$_POST['fname'],'<br>';

echo 'Email Address : ',$_POST['email'],'<br>'; $gend = ($_POST['gender']=='m')?'Male':'Female'; echo 'Gender : ',$gend,'<br>';

$a = $_POST['course']; echo $a,'<br><hr>';

} echo '<hr>';

echo 'The query string : ',$_SERVER['QUERY_STRING'],'<br>';

post.html

<form method='POST' action='post.php'/>

Name : <input type='text' name='fname' value=''/><br> E-Mail : <input type='text' name='email' value=''/><br>

Gender : <input type='radio' name='gender' value='m'/> Male <input type='radio' name='gender' value='f'/> Female <br>

Hobbies : <br>

<input type='checkbox' name='course[]' value='c'/> C-Language <br> <input type='checkbox' name='course[]' value='p'/> PHP <br>

<input type='checkbox' name='course[]' value='j'/> Java <br> <input type='submit' name='submit' value='Register'/>

</form>

Server variables: - servervariables.php

<?php

echo 'Get Environment : ',getenv('os'),'<br>'; echo 'Environment Path : ',getenv('path'),'<br>'; echo '<hr>';

echo 'Document Root : ',$_SERVER['DOCUMENT_ROOT'],'<br>'; echo 'Http Host : ',$_SERVER['HTTP_HOST'],'<br>';

echo 'Referer : ',$_SERVER['HTTP_REFERER'],'<br>'; echo 'Method : ',$_SERVER['REQUEST_METHOD'],'<br>'; echo 'User Agent : ',($_SERVER['HTTP_USER_AGENT']),'<br>'; echo 'Name of Script : ',$_SERVER['SCRIPT_NAME'],'<br>'; echo 'Query String : ',$_SERVER['QUERY_STRING'],'<br>'; echo 'Remote Ip Address : ',$_SERVER['REMOTE_ADDR'],'<br>'; echo 'PHP Self : ',$_SERVER['PHP_SELF'],'<br>';

echo 'Request URL : ',$_SERVER['REQUEST_URI'],'<br>';

echo 'Script File Name : ',$_SERVER['SCRIPT_FILENAME'],'<br>';

phpinfo.php

<?php

echo phpinfo();

Include_once and required _once: - these both will include file very first time and if already

file has been included this will not include for the second time.

Defference between include_once & include: - include and include _once will include the

files. But if the path of the file is wrong include and include_once will generate the wrong message and rest of the code will be excuted

Required and required_once: - if location of the path is given wrong this will through a wrong

as well as path error and code gets alted for the execution will be stop

Note: -include and require if you are calling a file you have to be predation whether the file

having are’t if function exist if you have to make function as it using function exist as a method

Ex: - a.php

<style>

</style> <b>I am included</b><br><hr> <?php $a = 10; if(!function_exists('r')){ function r(){

echo 'I am in a.php and working fine<br>'; } } ?> b.php <?php include_once("a.php"); include("a.php"); include("a.php"); $b = 20;

echo 'The Value of $a = ',$a,'<br>'; echo 'The Value of $b = ',$b,'<br>'; r();

?>

---

In document php - notes (Page 155-166)

Related documents