File Upload File and Directories
File Upload File and Directories
File Upload File and Directories
File System
In this tutorial you will learn how to create, access (or read) and manipulate files dynamically using
the PHP's file system functions.
If you try to open a file that doesn't exist, PHP will generate a warning message. So, to avoid these
error messages you should always implement a simple check whether a file or directory exists or not
before trying to access it, with the PHP file_exists() function.
Example
<?php
$file = "data.txt";
if(file_exists($file))
{ $handle = fopen($file, "r"); }
Else
{ echo "ERROR: File does not exist."; }
?>
<?php
$file = "data.txt";
// Check the existence of file
if(file_exists($file))
{ // Reading the entire file into a string
$content = file_get_contents($file) or die("ERROR: Cannot open the file.");
echo $content;
}
else
{ echo "ERROR: File does not exist."; }
?>
One more method of reading the whole data from a file is the PHP's file() function. It does a similar
job to file_get_contents() function, but it returns the file contents as an array of lines, rather than a
single string. Each element of the returned array corresponds to a line in the file.
To process the file data, you need to iterate over the array using a foreach loop. Here's an example,
which reads a file into an array and then displays it using the loop:
<?php
$file = "data.txt";
// Check the existence of file
if(file_exists($file))
{ // Reading the entire file into an array
$arr = file($file) or die("ERROR: Cannot open the file.");
foreach($arr as $line)
{ echo $line; }
}
else
{ echo "ERROR: File does not exist."; } ?>
<?php
$file = "note.txt";
$data = "The quick brown fox jumps over the lazy dog.";
file_put_contents($file, $data) or die("ERROR: Cannot write the file.");
echo "Data written to the file successfully.";
?>
<?php
$file = "note.txt";
$data = "The quick brown fox jumps over the lazy dog.";
file_put_contents($file, $data, FILE_APPEND) or die("ERROR: Cannot write the file.");
echo "Data written to the file successfully.";
?>
Function Description
Deleting a Directory
Directories are deleted in PHP using the rmdir() function. rmdir() takes a single argument, the
name of the directory to be deleted. The deletion will only be successful if the directory is empty. If
the directory contains files or other sub-directories the deletion cannot be performed until those files
and sub-directories are also deleted.
<?php
$current_dir = getCwd();
echo "Current directory is $current_dir";
?>
The current working directory can be changed using the chdir() function. chdir() takes as the only
argument the path of the new directory:
<?php
$current_dir = getCwd();
echo "Current directory is $current_dir <br>";
chdir ("/tmp");
$current_dir = getCwd();
echo "Current directory is now $current_dir <br>";
?>
<?php
chdir ("/tmp");
$current_dir = getCwd();
echo "Current directory is now $current_dir";
$array = scandir(".", 1);
print_r($array); ?>
PHP $_FILES
The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can get file name, file type, file siz
associated with file.
Here, we are assuming that file name is filename.
$_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.
$_FILES['filename']['error']
returns error code associated with this file.
The move_uploaded_file() function moves the uploaded file to a new location. The move_uploaded_file() function
checks internally if the file is uploaded thorough the POST request. It moves the file if it is uploaded through the
POST request.
Syntax
1. bool move_uploaded_file ( string $filename , string $destination )
<form action="uploader.php" method="post" enctype="multipart/form-data">
Select File:
<input type="file" name="fileToUpload"/>
<input type="submit" value="Upload Image" name="submit"/>
</form>
File: uploader.php
<?php
$target_path = "e:/";
$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!";
}
?>
1. Display image in PHP
<?php
$result = $_GET['image'];
echo '<img src="images/gallery/'.$result.'.jpg">';
?>
OR
<?php $result = $_GET['image']; ?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
</form>
<?php
if(isset($_POST['Submit1']))
{
$filepath = "images/" . $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath))
{
echo "<img src=".$filepath." height=200 width=300 />";
}
else
{
echo "Error !!";
}
}
?>
</body>
</html>
PHP mail is the built in PHP function that is used to send emails from PHP scripts.
<?php
mail($to_email_address,$subject,$message,[$headers],[$parameters]);
?>
HERE,
PHP mailer uses Simple Mail Transmission Protocol (SMTP) to send mail.
On a hosted server, the SMTP settings would have already been set.
The SMTP mail settings can be configured from “php.ini” file in the PHP installation folder.
Configuring SMTP settings on your localhost Assuming you are using xampp on windows, locate
the “php.ini” in the directory “C:\xampp\php”.
<?php
$to_email = 'name @ company . com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail function';
$headers = 'From: noreply @ company . com';
mail($to_email,$subject,$message,$headers);
?>