Breaking

Monday, July 9, 2018

Importants Function Of .PHP For Student And Beginner.

---File Read---

$file=fopen("abc.php","r");
fopen is used to provide the resource and the mode of resource is used with file. It accept 2 parameters, first parameter for resource and second for mode. It return the resource id.
Mode like read, read + write, append and write.
If file is not available, then it will throw an error, it won't create any file implicitly.

$content=fread($file, filesize("abc.php"));
fread is used to read the file and it returns the string. It accept two parameter, first parameter for resourceid and second for filesize.

--Print The Value Available In The File--

echo "$content";
echo "File read successfully";


fclose($file);
fclose is used to close the resource which we open earlier. It accept one parameter of resource id, which we want to close.


--File Write--

$file=fopen("new.txt", "w");
w Here is fopen work as same as above, but we open this file in write mode so we use w to write the resource. If data available in file, it will over write it with new data
fwrite($file, "Hello");
fwrite is used to write the file available in resource parameter. It accept two parameter, first parameter for resourceid and another for string you want to write in that file. It returns the number of character write in file.
If file is not available at that location, then it will create new file of the same name provided for resource.
echo fwrite($file, " Welcome to PHP Programing ");
echo "File write is comppleted"; fclose($file);



---File Append--

$file=fopen("newFile.txt", "a");
w Here we open this file in append mode so we use a to write the resource. Append is used to write data with existing data.
fwrite($file, "Hello");
If file is not available at that location, then it will create new file of the same name provided for resource.
echo fwrite($file, " Welcome to PHP Programing ");
echo "File write is comppleted";
fclose($file);



--FGetc & FGets function--

$r=fopen("new.txt", "r");
Open the file in read mode.

while (!feof($r)) {
echo fgetc($r);
}
fclose($r);
echo unlink("new.txt");


fgetc is used to read the file character by character where as fgets is used to read the file string by string.
In such case we need to run a loop which can execute a particular block of code for several time, based on condition.
Where feof Stands for Found End Of File, which means ou cursor placed at the bottom of while where our file get closed. or where nothing is available.
unlink is used to delete the file. It accept one parameter of file name.

No comments:

Post a Comment