Implementation of File Operations Creation of File, Open, Read, Write
Implementation of File Operations Creation of File, Open, Read, Write
Implementation of File Operations Creation of File, Open, Read, Write
21ID01IT021
<?php
$check = fopen("res.txt", "r");
fclose($check);
echo "HELLO!";
?>
<?php
$file_pointer = 'res.txt';
$open = file_get_contents($file_pointer);
$open .= "hello!";
file_put_contents($file_pointer, $open);
echo "content added";
?>
function. <?php
$file_pointer = "res.txt";
if (!unlink($file_pointer)) {
echo ("$file_pointer cannot be deleted due to an error");
}
else {
echo ("$file_pointer has been deleted");
}
?>
4. Write a PHP script to show the use of PHP Open File Modes.
<?php
$myfile = fopen("res.txt", "r") or die("Unable to
open file!");
echo fread($myfile,filesize("res.txt"));
fclose($myfile);
?>
5. Write a PHP script to read data of the file using fread() function.
<?php
$myfile = fopen("res.txt","r") or die("Unable to pen
file!");
echo fread($myfile, filesize("res.txt"));
fclose($myfile);
?>
<?php
$fp = fopen("res.txt","r");
echo fgets($fp);
fclose($fp);
?>
7. Write a PHP script to show the use of fputs() function.
<?php
$theFile = fopen("example.txt","a");
fputs($theFile,"line of text\n");
echo "content added";
?>
<?php
$fp = fopen("example.txt","r");
while(!feof($fp))
{
echo fgetc($fp);
}
fclose($fp);
?>
<?php
$i=0;
$fp = fopen("example.txt","r");
while(!feof($fp))
{
echo fgetc($fp);
$i++;
}
echo $i;
?>
9. Write a PHP script to show the use of fwrite() function.
<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
</body>
</html>