It is very important to know how to upload a file in PHP.File upload in PHP can used for uploading any types of files from a user computer to server. First it will store the file to a temporary folder in server and this file will be deleted when the user exist the script ( leaving that page ). And we can move that file to a permanent folder in server using move_uploaded_file() function.

First see how a HTML form will look like for file upload

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload" id="file" />
<input type="submit" name="submit" value="Upload file" />
</form>

This form will call the file upload.php where we will write the PHP scripts for handling this file upload.

$_FILES array :
$_FILES variable array will contain all the information about the uploaded file like its name, type, size and error codes.
See the different syntax and usage :

  • $_FILES["form field name for file"]["name"] : It will contain the name of the uploaded file
  • $_FILES["form field name for file"]["type"] : It will contain the mime type of the uploaded file
  • $_FILES["form field name for file"]["size"] : It will contain the size of the uploaded file
  • $_FILES["form field name for file"]["tmp_name"] : It will contain the name with path of the temporary folder in the server.

Now we can write a sample PHP program using the same html form above

<?php
if ($_FILES["fileupload"]["error"] > 0)
{
echo "Error code when upload: " . $_FILES["fileupload"]["error"] . "<br />";
}
else
{
echo "Upload file name : " . $_FILES["fileupload"]["name"] . "<br />";
echo "Type of the file: " . $_FILES["fileupload"]["type"] . "<br />";
echo "Size of the file in KB: " . ($_FILES["fileupload"]["size"] / 1024) . "<br />";
echo "Stored in temporary folder : " . $_FILES["fileupload"]["tmp_name"];
}
?>

Its only a temporary storage now, the file will be deleted when we quit the script. If we need to store the file in server for later use, we need to move that file from temporary folder to a permanent folder ( lets us take a folder “uploads” for this example ). Syntax of the move_uploaded_file(0 function is like
move_uploaded_file( source file, destination file )

Now watch the sample PHP program for file upload to a server and store the file to a permanent folder uploads

<?php
if ($_FILES["fileupload"]["error"] > 0)
{
echo "Error code when upload: " . $_FILES["fileupload"]["error"] . "<br />";
}
else
{
echo "Upload file name : " . $_FILES["fileupload"]["name"] . "<br />";
echo "Type of the file: " . $_FILES["fileupload"]["type"] . "<br />";
echo "Size of the file in KB: " . ($_FILES["fileupload"]["size"] / 1024) . "<br />";
echo "Stored in temporary folder : " . $_FILES["fileupload"]["tmp_name"];
move_uploaded_file($_FILES["fileupload"]["tmp_name"],"uploads/".$_FILES["file"]["name"]);
}
?>

Now our uploaded file is stored in the folder named “uploads” in server.

And this is only a basic introduction about file upload, normally we need to do several other checking and operations before the file upload as per the program requirement. For example if its an image upload section in site, we need to check the uploaded file type is image before storing. And we can restrict the maximum size using the $_FILES["form field name for file"]["size"] variable. And we can also rename the file before storing. I will explain those things in later posts.

PHP fread() function is used for reading contents of a file. It read from the file stream pointed to by handle which is created by fopen() function.
Syntax of the PHP fwrite function :
fread( file handler, length )

Length is a parameter to specify how many bytes to be read. Read will stop at the specified length is reached.

PHP fread() statement is binary-safe , means both binary and character data can be read from a file.

Here is a sample PHP program for reading from a file using fread() function

<?php
// First we will create a file syam.txt and write a string into that
$file = fopen("syam.txt","w");
fwrite($file,"Here You can learn about how to write to a file");
fclose($file);
// Now we are going to read the contents of the created file and display the contents
$file_1 = fopen("syam.txt","r");
$data=fread($file_1,filesize("syam.txt"));
echo $data;
fclose($file_1);
?>

Its output will be :

Here You can learn about how to write to a file

This is only a basic introduction tutorial about reading a file using PHP. We will discuss more about in our later posts.

PHP fwrite() function is used for writing contents to a file. It write to the file stream pointed to by handle which is created by fopen() function.

Syntax of the PHP fwrite function :
fwrite( file handler, string to write , length )

Length argument is optional , and if we specify a length parameter write will stop when it write will reach to that length bytes.

PHP write() statement is binary-safe , means both binary and character data can be written to a file.

Here is a sample PHP program for writing to a file using fwrite() function

<?php
$file = fopen("syam.txt","w");
fwrite($file,"Here You can learn about how to write to a file");
fclose($file);
?>

This program will create syam.txt if not exist using fopen statement and write the string “Here You can learn about how to write to a file” into that file using the handler $file and fwrite() statement.

Its only a basic tutorial about writing to files. We will discuss more about this in our later posts.

PHP fopen() function is used for opening a file or url in PHP. It can also be used for creating a file in PHP.We can use two parameters with this function. First parameter is for specifying the file name and second parameter is for specifying the mode for opening that file.

Syntax of the fopen() function is

$file=fopen(“file name”,”mode”);

for ex:

$file=fopen(“syam.txt”,”r”);

See the different opening modes used with fopen()

  • r : Open for read only, Start from the beginning of file
  • r+ : open for read and write , start from the beginning of file
  • w : Open for write only, if the file exists all the data will be removed and start from the beginning, if the file not exist – it will create the new file
  • w+ : Open for write and read, if the file exists all the data will be removed and start from the beginning, if the file not exist – it will create the new file
  • a : Open for write only , mainly for append operation, will point to the end of file , will create a new file if file not exist
  • a+ : Open for read and write, will point to the end of file , will create a new file if file not exist
  • x : create and open for write only , starts from the beginning of file, if the file exist E_WARNING error will occur, if the file does not exist it will create a new file
  • x+ : create and open for read and write, starts from the beginning of file, if the file exist E_WARNING error will occur, if the file does not exist it will create a new file

From this we can see that following modes are used for creating a file in PHP

  • a
  • a+
  • w
  • w+
  • x
  • x+