PHP Library Functions for Uploading Files

0
40

In fact, there are several PHP Library Functions for Uploading Files that we use in our script. The following section discusses these functions in brief.

Brief Introduction to PHP Library Functions for Uploading Files

file_exists ()

In order to find whether a file or directory exists or we do not use this function. Before uploading a file, we need to check whether the corresponding directory exists or not. Also, it also helps us decide whether to overweight the file if it exists, or not. The file_exists () method takes a parameter of string type indicating the file name and returns a boolean value of true or false. The following code example shows how to use the file_exists () function. For more details, refer to the PHP Manual: https://www.php.net/manual/en/function.file-exists.php.

<?php
   $dir="uploads/";
   $b=file_exists($dir);
   if($b)
	echo "The directory ", $dir, " exists";
   else
	echo "The directory ", $dir, " does not exist";
?>

Output

Checking a Directory using file_exists () Function

Similarly, we can find whether a file already exists or not. The following code demonstrates it.

<?php
   $file="fileexs.php";
   $b=file_exists($file);
   if($b)
	echo "The file ", $file, " exists";
   else
	echo "The file ", $file, " does not exist";
?>

Output

Checking Whether a File Exists or Not in PHP
Checking Whether a File Exists or Not in PHP

pathinfo ()

In order to find the information about the path of a file, we use the pathinfo () function. This function returns different components of the specified file as an array. The function takes two parameters. While the first one is mandatory and represents the name of the file or directory. The second parameter is optional and represents the name of a flag. Further, the flag represents one of the component of the array that this function returns. Accordingly, the flags include – PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION, and PATHINFO_FILENAME. Evidently, these flags represent the directory name, basename, file name, and extension (in case it is a file) respectively. When you pass the path of a file as a parameter, then it returns all of these four values ​​in an array. HowevThe following example demonstrates it.

<?php
   $path="/p/Files/fileexs.php";
   $info=pathinfo($path);
   print_r($info);
?>

Output

Display the file path information using the pathinfo () function in PHP
Display the file path information using the pathinfo () function in PHP

For more details on the pathinfo () function, refer to the PHP manual: https://www.php.net/manual/en/function.pathinfo.php

The following example demonstrates the use of both of the functions discussed above.

<?php
   $path1="/p/Files/fileexs.php";
   $path2="/p/Files/uploads/";
   $info1=pathinfo($path1);
   $info2=pathinfo($path2);
  
   if(file_exists($info1['basename']))
   echo 'Path Information of the file: <br>';
   foreach($info1 as $k=>$v)
   
	echo $k,'=>',$v,'<br>';
   

   if(file_exists($info2['basename']))
   echo '<br>Path Information of the Directory: <br>';
   foreach($info2 as $k=>$v)
   
	echo $k,'=>',$v,'<br>';
   

?>

Output

Examples of file_exists () and pathinfo () Functions
Examples of file_exists () and pathinfo () Functions

Basically, the file upload requires calling the move_uploaded_file () function. Click here to find the details and examples of the move_uploaded_file () function. (https://www.programmingempire.com/a-brief-description-of-move_uploaded_file-function-in-php/)


Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

princites.com

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here