Vous êtes sur la page 1sur 16

File Handling with PHP

Opening a File
The fopen() function is used to open files in PHP.
The first parameter of this function contains the name of the file to

be opened
the second parameter specifies in which mode the file should be
opened:
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
?>

File Open Modes


r

Open for reading only. Start at beginning of


file.
r+ Open for reading and writing. Start at
beginning of file.
w
Open for writing only. Remove all previous
content, if file doesnt exist, create it.
a
Open writing, but start at END of current
content.
a+ Open for reading and writing, start at END
and create file if necessary.

Closing a File
The fclose() function is used to close an open
file:
<?php
$file = fopen("test.txt","r");
//some code to be executed

fclose($file);
?>

Check End-of-file
The feof() function checks if the "end-of-file"
(EOF) has been reached.
The feof() function is useful for looping
through data of unknown length.
Note: You cannot read from files opened in w,
a, and x mode!
if (feof($file)) echo "End of file";

Writing to file
fwrite() writes to an open file.
The function will stop at the end of the file or when it reaches the
specified length, whichever comes first.
This function returns the number of bytes written, or FALSE on failure.
Syntax
fwrite(file,string,length)
Parameter

Description

file

Required. Specifies the open file to write to

string

Required. Specifies the string to write to the open file

length

Optional. Specifies the maximum number of bytes to


write

Example
<?php
$file = fopen("test.txt","w");
fwrite($file,"Hello World. Testing!");
fclose($file);
?>

Fputs
The fputs() writes to an open file.
This function returns the number of bytes written on success,
or FALSE on failure.
The fputs() function is an alias of the fwrite() function.
Syntax
fputs(file,string,length)
Parameter

Description

file

Required. Specifies the open file to write to

string

Required. Specifies the string to write to the open file

length

Optional. Specifies the maximum number of bytes to write

<?php
$file = fopen("test.txt","w");
fputs($file,"Hello World. Testing!");
fclose($file);
?>

Reading a File Line by Line


The fgets() function is used to read a single line from a file.
Note: After a call to this function the file pointer has moved to
the next line.
Example
<?php
$file1 =fopen("test.txt","r");
if(!file1)
echo "Unable to open file!";
else
{
while(!feof($file1))
{$line=fgets($file1) ;
echo $line. "<br>";
}
fclose($file1);} ?>

Reading a File Character by Character


The fgetc() function is used to read a single character from a file.
Note: After a call to this function the file pointer moves to the next
character.
Example
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>

PHP - File Upload: HTML Form

<form enctype="multipart/form-data" action="uploader.php"


method="POST">

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

Choose a file to upload: <input name="uploadedfile" type="file" /><br />

<input type="submit" value="Upload File" /></form>

enctype="multipart/form-data" - Necessary for our to-be-

created PHP file to function properly.


action="uploader.php" - The name of our PHP page that
will be created, shortly.

method="POST" - Informs the browser that we want to send


information to the server using POST.
input type="hidden" name="MAX... - Sets the maximum

allowable file size, in bytes, that can be uploaded.. We have set


the max file size to 100KB in this example.
input name="uploadedfile" - uploadedfile is how we will
access the file in our PHP script.

Uploader.php
<?php
$target_path = "uploads/";
$target_path = $target_path . basename(
$_FILES['uploadedfile']['name']);
if (file_exists("uploads/" . $_FILES['uploadedfile']["name"]))
{
echo $_FILES['uploadedfile']["name"] . " already exists. " ;
}
else
{ move_uploaded_file($_FILES['uploadedfile']['tmp_name'],
$target_path);
echo "The file ". basename( $_FILES['uploadedfile']['name']). "
has been uploaded;}
?>

$_FILES Array
An associative array of items uploaded to the current
script via the HTTP POST method.
$_FILES['file_field_name']['name']
The original name of the file on the client machine.

$_FILES['file_field_name']['type']
The mime type of the file, if the browser provided this
information. An example would be "image/gif". This mime
type is however not checked on the PHP side and
therefore don't take its value for granted.

$_FILES['file_field_name']['size']
The size, in bytes, of the uploaded file.

$_FILES['file_field_name']['tmp_name']
The temporary filename of the file in which the uploaded
file was stored on the server.

$_FILES['file_field_name']['error']
The error code associated with this file upload. This
element was added in PHP 4.2.0

Vous aimerez peut-être aussi