Professional Documents
Culture Documents
03-Files 10302014
03-Files 10302014
PHP Workshop
PHP Workshop
Open/Close a File
A file is opened with fopen() as a stream,
and PHP returns a handle to the file that
can be used to reference the open file in
other functions.
Each file is opened in a particular mode.
A file is closed with fclose() or when your
script ends.
PHP Workshop
if (file_exists("names.txt"))
echo "names.txt exists\n";
names.txt exists
PHP Workshop
PHP Workshop
Now what..?
If you open a file to read, you can use
more in-built PHP functions to read data..
If you open the file to write, you can use
more in-built PHP functions to write..
PHP Workshop
Reading Data
There are two main functions to read data:
fgets($handle,$bytes)
Reads up to $bytes of data, stops at newline
fread($handle,$bytes)
Reads up to $bytes of data, stops at EOF.
d) abcdefg
12
e) None of above
PHP Workshop
PHP Workshop
Reading Data
We need to be aware of the End Of File
(EOF) point..
feof($handle)
Whether the file has reached the EOF point.
Returns true if have reached EOF.
PHP Workshop
fclose($handle);
PHP Workshop
fclose($handle);
PHP Workshop
fclose($handle);
PHP Workshop
fclose($handle);
fclose($handle);
PHP Workshop
$lines = file($filename)
Reads entire file into an array with each line a
separate entry in the array.
$str = file_get_contents($filename)
Reads entire file into a single string.
PHP Workshop
Writing Data
To write data to a file use:
fwrite($handle,$data)
Write $data to the file.
PHP Workshop
PHP Workshop
$handle
$handle == fopen('people.txt',
fopen('people.txt', 'a');
'a');
fwrite($handle,
fwrite($handle, \nFred:Male);
'\nFred:Male');
fclose($handle);
PHP Workshop
Copy file
copy('source', 'destination');
Locking files
<?php
$fh = fopen(testfile.txt, r+) or die(Failed to open file);
$text = fgets($fh);
if(flock($fh, LOCK_EX)){
fseek($fh, 0, SEEK_END);
fwrite($fh, $text) or die(could not write to file);
flock($fh, LOCK_UN);
}
fclose($fh);
echo File testfile.txt successfully updated;
?>
PHP Workshop
$_FILES usage
Uploading file
<html><head><title>PHP form upload</title></head><body>
<form method='post' action='question2.php' enctype='multipart/form-data'>
Select File: <input type='file' name='filename' size='10' />
<input type='submit' value='Upload' />
</form>
<?php
if($_FILES){
$name = $_FILES['filename']['name'];
move_uploaded_file($_FILES['filename']['tmp_name'],$name);
echo "Uploaded image '$name'<BR><img src='$name'/>";
}
?>
PHP Workshop
application/pdf
image/gif
multipart-form-data
text/xml
application/zip
image/jpeg
text/css video/mpeg
audio/mpeg
PHP Workshop
image/png
text/html
video/mp4
audio/x-wav
image/tiff
text/plain
video/quicktime
Close a directory
closedir($handle)
Closes directory 'stream'
PHP Workshop
Directory Example
$handle = opendir('./');
while(false !== ($file=readdir($handle)))
{
echo "$file<br />";
}
closedir($handle);
PHP Workshop
Directory Example
$handle
$handle == opendir('./');
opendir('./');
Open current directory
closedir($handle);
PHP Workshop
Directory Example
$handle = opendir('./');
while(false !==
!== ($file=readdir($handle)))
($file=readdir($handle)))
while(false
{ {
echo
"$file<br
/>";
echo
"$file<br
/>";
} }
closedir($handle);
PHP Workshop
Directory Example
$handle = opendir('./');
while(false !== ($file=readdir($handle)))
{
echo "$file<br />";
}
closedir($handle);
closedir($handle);
Close the directory stream
PHP Workshop
Change Directory
chdir('dirname');
Create directory
mkdir('dirname');
And more!
www.php.net/manual/en/ref.dir.php
PHP Workshop
Review