not logged in • loginregister

Forums


Forums Index » Computers » PHP » Manipulating files

Manipulating files


Go to Page: 1


 
August 10th, 2005 15:46
#1

robert_ak


robert_ak's avatar








Super Administrator

Registered: 20/07/05 @ 17:00
Posts: 2168


A little tutorial on how to manipulate text files.

CREATE A FILE:

<?php
// create the file (notice the w for write)
$fp = fopen("test.txt", "w&quot;

// insert contents into file
fputs($fp, "hello!&quot;

// close the file
fclose($fp);

echo "The file has been created.";
?>


***

OPEN A FILE:

<?php
// open the file for reading (notice the r for read)
$fp = fopen("test.txt", "r&quot;

// take the contents
$contents = fread($fp, filesize("test.txt&quot);

// close the file
fclose($fp);

// output file contents
echo $contents;
?>


***

DELETE A FILE:

<?php
// erase the file
unlink("test.txt&quot;
?>


You might also want to see if the file exists before trying deleting it:

<?php
$file = "test.txt";

// check if file exists
if(file_exists($file)) {
// delete it if it does
unlink($file);
} else {
echo "Error: You are trying to delete a file that does not exist.";
}
?>

______________________
hai
Back to top


Go to Page: 1



Forums Index » Computers » PHP » Manipulating files

Jump to:   

Who's Browsing This Topic?

1 guest.


Move to: