File Uploading in PHP

 

File Uploading in PHP


PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variable i.e. $_FILES, it contains all information about file.By the help of $_FILES global variable, we can get file name, file type, file size and temparary file name associated with file.


  • $_FILES['filename']['name'] - returns file name.
  • $_FILES['filename']['type'] - returns MIME type of the file.
  • $_FILES['filename']['size'] - returns size of the file (in bytes).
  • $_FILES['filename']['tmp_name'] - returns temporary file name of the file which was stored on the server.

In HTML, File Upload control can be created by using following:

<input type="file" name="fileupload"/>


In PHP, To upload the file we have to use the function called move_uploaded_file().

 move_uploaded_file() function

The move_uploaded_file() function is used to move the uploaded file to a new location. It moves the file only if it is uploaded through the POST request.

Syntax:
  move_uploaded_file ( string $filename , string $destination )


First Configure the php.ini File by ensure that PHP is configured to allow file uploads. In your php.ini file, search for the file_uploads directive, and set it to On i.e. file_uploads = On

Example: "fileupload.php"


<html>
<head>
<title>File Upload</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">  
    Select File: <input type="file" name="fileToUpload"/>  
    <input type="submit" value="Upload Any File" name="submit"/>  
</form>
<?php  
if(isset($_POST["submit"]))
{
$target_path = "D:/";  
$target_path=$target_path.basename($_FILES['fileToUpload'] ['name'] );  

if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) 
{  
    echo "File uploaded successfully!";  
} 
else
{  
    echo "Sorry, file not uploaded, please try again!";  
}
}
?>  
</body>
</html>



Output :

image

netaji gandi Monday, November 25, 2024
File Handling in PHP

 

File Handling in PHP


In PHP File System allows us to create file, read file line by line, read file character by character, write file, append file, delete file and close file.

PHP supports following functions to handle files.

  • fopen()
  • fread()
  • fwrite()
  • fclose()
  • unlink()

 fopen()

In PHP, The fopen() function is used to open a file in read mode or write mode or in both read and write modes. And this function accepts two arguments: $filename and $mode.


Syntax:    fopen(string $filename , string $mode)


The $filename represents the file to be opended and $mode represents the file mode for example read-only, read-write, write-only etc.


The fopen() supports the following modes:

r-Read Only
w-Write Only
a-Append
r+-Read & Write.
w+-Read & Write.

Example: "fopen.php"


<?php    
$filename = "E:\\netaji\\simple.txt";    
$fp = fopen($filename, "r");//open file in read mode
?>


  Note : The above code will open the file in in read mode.

 fread()

In PHP, The fread() function is used to read data of the file. It requires two arguments: file resource and file size.


Syntax:    fread(resource $fp,int $length)


Where $fp represents file pointer that is created by fopen() function. And $length represents length of file (bytes)to be read.

Example: "fread.php"


<?php    
$filename = "D:\\Netaji\\file.txt";    
$fp = fopen($filename, "r");//open file in read mode    
$content = fread($fp, filesize($filename));//read file    
echo "$content";//printing data of file  
fclose($fp);//close file    
?>


  Note : To read content of file, we need to open file with r , r+, w+ mode.

Output :


 fwrite()

In PHP, The fwrite() function is used to write the content (string) into file.And It requires two arguments: file resource and data(string)

If the file opened in write(w) mode, the fwrite() function will erase the previous data of the file and writes the new data or If the file opened in append(a) mode, the fwrite() function appends the new data at the end of the previous data.

Syntax:    fwrite(resource $fp,string $data)


Where $fp represents file pointer that is created by fopen() function.And $data represents the data to be written.


 If the file opened in write(w) mode :

Example: "fwrite.php"


<?php    
$filename = "D:\\netaji\\file.txt";  
$fp = fopen($filename, "w");//open file in write mode    
fwrite($fp,'PHP is a Scripting Language');
echo "Data written successfully..";
fclose($fp);//close file    
?>


Output :

image

Text File :

image



 If the file opened in append(a) mode :

Example: "fappend.php"


<?php    
$filename = "D:\\netaji\\file.txt";    
$fp = fopen($filename, "a");//open file in write mode    
fwrite($fp,' at server side');
echo "Data appended successfully..";
fclose($fp);//close file    
?>


Output :

image

Text File :

image

  Note : To write data into file, you need to use w,r+,w+ mode.

 fclose()

In PHP, The fclose() function is used to close an open file.And It requires one argument i.e. "File Pointer"


Syntax:    fclose(resource $fp)


Where $fp represents file pointer that is created by fopen() function.

Example: "fclose.php"


<?php    
$filename = "D:\\netaji\\file.txt";  
$fp = fopen($filename, "r");//open file in write mode    
// To close the file 
fclose($fp);   
?>


 unlink()

In PHP, The unlink() function is used to delete any file.And It requires one argument i.e. "File Name"


Syntax:    unlink(string $filename)


Where $filename represents name of the file to be remove.

Example: "unlink.php"


<?php
$status=unlink('file.txt');
if($status){
echo "File deleted successfully";
}else{
echo "Sorry!";
}
?>


Output :

image



netaji gandi

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...