Delete file in PHP
PHP Tutorial » Delete file in PHP
How do I delete a file in PHP
Deleting a file means completely deleting a file from a directory so that the file no longer exists. PHP has a function named unlink () that allows you to delete a file.
Syntax:
unlink( $file_name, $context );
?>
Where:
The unlink() function in PHP takes two parameters as: $file_name and $context.
unlink() function to remove file from directory.
Suppose we have a file in the database named "my_file.txt"
<?php
// PHP program to delete my_file.txt
// using unlike() function
$file_to_delet = "my_file.txt";
// Use unlink() function to delete a file
if (!unlink($file_to_delet)) {
echo ("$file_to_delet - file cannot be deleted due to an error");
}
else {
echo ("$file_to_delet - file has been deleted");
}
?>
Related subjects:
PHP for loop
PHP foreach
What is an array
Convert array to string comma
Array to comma separated
In this chapter we go deeper intoo:
How do I delete a file in PHP?
Which function is used to delete the file in PHP?
What is unlink in PHP?
How do you empty a folder in PHP?
How do you delete a directory which is not empty?
What is the function of rmdir?
Tags: php delete file: if exists, from server, from directory, after download, from folder, on server, contents
after time, starting with, function
How to delete a directory.
Sometime, we need to delete a directory using php code. so in this post i will help you how to remove a directory named "php_images".
<?php
$dir_path = "php_images";
if(!rmdir($dir_path)) {
echo ("Could not remove $dir_path");
}
else {
echo ("$dir_path - file has been deleted");
}
?>
How can I delete the contents of a file in PHP?
To delete the content of the file using preg_replace() function.
Syntax:
preg_replace( $pattern, $replacement, $subject);
<?php
$my_file = 'fruits.txt';
$content_file = file_get_contents('fruits.txt');
echo "File contents before using "
. "preg_replace() function<br>";
echo $content_file;
echo "<br><br>File contents after using "
. "preg_replace() function<br> ";
$file_empty = preg_replace('/[a-z]/', '', $content_file);
echo $file_empty;
file_put_contents($my_file, $file_empty);
?>
php delete file: if exists, from server, from directory, after download, from folder, on server, contents
after time, starting with, function
Delete file in PHP - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.