0% found this document useful (0 votes)
9 views17 pages

IP2 Chap3

Chapter 3 covers file and directory handling in PHP, detailing functions for opening, reading, writing, and uploading files. It explains file permissions, the use of fopen(), fread(), fwrite(), and the process of file uploading through HTML forms. Additionally, it provides code examples for creating upload forms and handling uploaded files using the $_FILES global variable.

Uploaded by

Melak Enchalew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views17 pages

IP2 Chap3

Chapter 3 covers file and directory handling in PHP, detailing functions for opening, reading, writing, and uploading files. It explains file permissions, the use of fopen(), fread(), fwrite(), and the process of file uploading through HTML forms. Additionally, it provides code examples for creating upload forms and handling uploaded files using the $_FILES global variable.

Uploaded by

Melak Enchalew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 3

Files and Directories


PHP File
A file is a sequence of bytes stored on the
storage media such as hard disk.
PHP provides you with many useful functions
that allow you to handle files effectively.
PHP File Permission
File permissions specify what you can do with a
particular file in the system e.g., reading, writing
or executing the file.
Notice that PHP automatically grants
appropriate permissions behind the scenes.
For example, if you create a new file for writing,
PHP automatically grants read and write
permissions to you.
Opening and Closing Files

The PHP fopen() function is used to open a


file.
It requires two arguments stating first the
file name and then mode in which to operate.

Files modes can be specified as one of the


six options in this table.
Cont’d…
Cont’d…
If an attempt to open a file fails, then fopen
returns a value of false otherwise
It returns a file pointer which is used for
further reading or writing to that file.
Reading a file
Once a file is opened using fopen() function it can
be read with a function called fread().
This function requires two arguments.
These must be the file pointer and the length of the
file expressed in bytes.
Cont’d…

The file's length can be found using the


filesize() function which takes the file name
as its argument and returns the size of the
file expressed in bytes.
So here are the steps required to read a file with
PHP.
Open a file using fopen() function.
Get the file's length using filesize() function.
Read the file's content using fread() function.
Close the file with fclose() function.
Cont’d…
<html> {
<head> echo ( "Error in opening
<title>Reading a file file" );
using PHP</title> exit();
</head> }
<body> $filesize =
<?php filesize( $filename );
$filename = $filetext = fread( $file,
"/tmp.txt"; $filesize );
$file = fclose( $file );
fopen( $filename, echo ( "File size : $filesize
"r" ); bytes" );
if( $file == false ) echo (
"<pre>$filetext</pre>" );
Writing a File

A new file can be written or text can be


appended to an existing file using the PHP
fwrite()function.
This function requires two arguments
specifying a file pointer and the string of
data that is to be written.
Cont’d…
<?php
$filename = $filename =
"c:/xampp/htdocs/write.txt";
$file = fopen( $filename, "w" );
if( $file == false )
{
echo ( "Error in opening new file" );
exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );
?>
File Uploading
A PHP script can be used with a HTML form to allow
users to upload files to the server.
Initially files are uploaded into a temporary
directory and then relocated to a target destination
by a PHP script.
The process of uploading a file follows these steps:
The user opens the page containing a HTML form
featuring a text files, a browse button and a
submit button.
The user clicks the browse button and selects a
file to upload from the local PC.
Cont’d…
The full path to the selected file appears in the
text field, then the user clicks the submit
button.
The selected file is sent to the temporary
directory on the server.
The PHP script that was specified as the form
handler in the form's action attribute checks
that the file has arrived and then copies the file
into an intended directory.
The PHP script confirms the success to the user.
Creating an Upload Form
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="/php/fupload.php" method="post"
enctype="multipart/form-data">
<input type="file" name=“image" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
Cont’d…
Creating an upload script

There is one global PHP variable called


$_FILES.
$_FILES[‘image']['tmp_name']- the uploaded file
in the temporary directory on the web server.
$_FILES[‘image']['name'] - the actual name of the
uploaded file.
$_FILES[‘image']['size'] - the size in bytes of the
uploaded file.
$_FILES[‘image']['type'] - the MIME type of the
<?php
if(isset($_FILES['image'])){
$f_name = $_FILES['image']['name'];
$f_size = $_FILES['image']['size'];
$f_tmp = $_FILES['image']
['tmp_name'];
$f_type = $_FILES['image']['type'];
$f_exn= array("jpeg","jpg","png");
if(array($f_exn))

move_uploaded_file($f_tmp,'images/'.
$f_name);
{
echo "File uploaded successfully";
}}
else {
Cont’d…
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo
$_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']
['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']
['type']; ?>
</ul>
</body>
</html>
Thank you!

You might also like