AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Delete file in PHP


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

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:

<?php
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"

Example (this is HTML - PHP editor, change text on this window)
<?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".

Example (this is HTML - PHP editor, change text on this window)
<?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);

Example (this is HTML - PHP editor, change text on this window)
<?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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.