Cara menggunakan unlink di php

Kawan pernah mengupload data dalam database kemudian menyimpan file kedalam folder?????? Nah bagaimana kalau ingin menghapus data itu????

YA….pakai UNLINK . Tujuanya adalah menghapus data didalam database dan data dalam folder yang telah kita tentukan.

Misalkan saya punya Tabel : Product

Field : id, code,name,fileTipe

Langsung saja kawan,

ini script Unlink di php:

<!--?
php include "../config.php"; // membaca id file yang akan dihapus
$id  = $_GET['id'];
$code=$_GET['code'];
// membaca nama file yang akan dihapus berdasarkan id
 $query   = "SELECT * FROM product  WHERE code='$code' and id = '$id'";
$hasil   = mysql_query($query);
$data    = mysql_fetch_array($hasil);
 $namaFile = $data['name'];

// query untuk menghapus informasi file berdasarkan id
$query = "DELETE FROM product  where id = $id";
 mysql_query($query);
// menghapus file dalam folder sesuai namanya
unlink('NamaFolderAnda/'.$namaFile);
 echo "File telah dihapus";
 ?-->

Ok….Jadi kawan…Selamat Mencoba..
Hahahahahhaa….


<Lengkap PHP Filesystem Referensi


Definisi dan Penggunaan

The unlink() fungsi menghapus file.

Fungsi ini mengembalikan TRUE pada keberhasilan, atau FALSE pada kegagalan.

Sintaksis


Parameter Deskripsi
filename Wajib. Menentukan file untuk menghapus
context Pilihan. Menentukan konteks menangani file. Konteks adalah serangkaian pilihan yang dapat memodifikasi perilaku sungai

Contoh

<?php
$file = "test.txt";
if (!unlink($file))
  {
  echo ("Error deleting $file");
  }
else
  {
  echo ("Deleted $file");
  }
?>


<Lengkap PHP Filesystem Referensi

(PHP 4, PHP 5, PHP 7, PHP 8)

unlinkDeletes a file

Description

unlink(string $filename, ?resource $context = null): bool

Parameters

filename

Path to the file.

If the file is a symlink, the symlink will be deleted. On Windows, to delete a symlink to a directory, rmdir() has to be used instead.

context

A context stream resource.

Return Values

Returns true on success or false on failure.

Changelog

VersionDescription
7.3.0 On Windows, it is now possible to unlink() files with handles in use, while formerly that would fail. However, it is still not possible to re-create the unlinked file, until all handles to it have been closed.

Examples

Example #1 Basic unlink() usage

<?php
$fh 
fopen('test.html''a');
fwrite($fh'<h2>Hello world!</h2>');
fclose($fh);unlink('test.html');
?>

See Also

  • rmdir() - Removes directory

anagai at yahoo dot com

10 years ago

This will delete all files in a directory matching a pattern in one line of code.

<?php array_map('unlink', glob("some/dir/*.txt")); ?>

dexen dot devries at gmail dot com

11 years ago

Deleted a large file but seeing no increase in free space or decrease of disk usage? Using UNIX or other POSIX OS?

The unlink() is not about removing file, it's about removing a file name. The manpage says: ``unlink - delete a name and possibly the file it refers to''.

Most of the time a file has just one name -- removing it will also remove (free, deallocate) the `body' of file (with one caveat, see below). That's the simple, usual case.

However, it's perfectly fine for a file to have several names (see the link() function), in the same or different directories. All the names will refer to the file body and `keep it alive', so to say. Only when all the names are removed, the body of file actually is freed.

The caveat:
A file's body may *also* be `kept alive' (still using diskspace) by a process holding the file open. The body will not be deallocated (will not free disk space) as long as the process holds it open. In fact, there's a fancy way of resurrecting a file removed by a mistake but still held open by a process...

deen804 at gmail dot com

8 years ago

unlink($fileName); failed for me .
Then i tried using the realpath($fileName)  function as
unlink(realpath($fileName)); it worked

just posting it , in case if any one finds it useful .

federico at poisonfx dot com

11 years ago

Here the simplest way to delete files with mask

<?php
   $mask
= "*.jpg"
  
array_map( "unlink", glob( $mask ) );
?>

chris at vibenewmedia dot com

18 years ago

To delete all files of a particular extension, or infact, delete all with wildcard, a much simplar way is to use the glob function.  Say I wanted to delete all jpgs .........

<?phpforeach (glob("*.jpg") as $filename) {
   echo
"$filename size " . filesize($filename) . "\n";
  
unlink($filename);
}
?>

PD

14 years ago

I have been working on some little tryout where a backup file was created before modifying the main textfile. Then when an error is thrown, the main file will be deleted (unlinked) and the backup file is returned instead.

Though, I have been breaking my head for about an hour on why I couldn't get my persmissions right to unlink the main file.

Finally I knew what was wrong: because I was working on the file and hadn't yet closed the file, it was still in use and ofcourse couldn't be deleted :)

So I thought of mentoining this here, to avoid others of making the same mistake:

<?php
// First close the file
fclose($fp);// Then unlink :)
unlink($somefile);
?>

gotdalife at gmail dot com

14 years ago

To anyone who's had a problem with the permissions denied error, it's sometimes caused when you try to delete a file that's in a folder higher in the hierarchy to your working directory (i.e. when trying to delete a path that starts with "../").

So to work around this problem, you can use chdir() to change the working directory to the folder where the file you want to unlink is located.

<?php
    $old
= getcwd(); // Save the current directory
   
chdir($path_to_file);
   
unlink($filename);
   
chdir($old); // Restore the old working directory   
?>

Eric

8 years ago

This might seem obvious, but I was tearing my hair out with this problem - make sure the file you're trying to delete isn't currently being used. I had a script that was parsing a text file and was supposed to delete it after completing, but kept getting a permission denied error because I hadn't explicitly closed the file, hence it was technically still being "used" even though the parsing was complete.

llmll at gmx dot de

7 years ago

On OSX, when fighting against a "Permission Denied" error, make sure, the directory has WRITE permissions for the executing php-user.

Furthermore, if you rely on ACLs, and want to delete a file or symlink, the containing directory needs to have "delete_child" permission in order to unlink things inside. If you only grant "delete" to the folder that will allow you to delete the container folder itself, but not the objects inside.

divinity76+spam at gmail dot com

1 month ago

if you're looking for a recursive unlink:
<?php/**
* delete a file or directory
* automatically traversing directories if needed.
* PS: has not been tested with self-referencing symlink shenanigans, that might cause a infinite recursion, i don't know.
*
* @param string $cmd
* @throws \RuntimeException if unlink fails
* @throws \RuntimeException if rmdir fails
* @return void
*/
function unlinkRecursive(string $path, bool $verbose = false): void
{
    if (!
is_readable($path)) {
        return;
    }
    if (
is_file($path)) {
        if (
$verbose) {
            echo
"unlink: {$path}\n";
        }
        if (!
unlink($path)) {
            throw new \
RuntimeException("Failed to unlink {$path}: " . var_export(error_get_last(), true));
        }
        return;
    }
   
$foldersToDelete = array();
   
$filesToDelete = array();
   
// we should scan the entire directory before traversing deeper, to not have open handles to each directory:
    // on very large director trees you can actually get OS-errors if you have too many open directory handles.
   
foreach (new DirectoryIterator($path) as $fileInfo) {
        if (
$fileInfo->isDot()) {
            continue;
        }
        if (
$fileInfo->isDir()) {
           
$foldersToDelete[] = $fileInfo->getRealPath();
        } else {
           
$filesToDelete[] = $fileInfo->getRealPath();
        }
    }
    unset(
$fileInfo); // free file handle
   
foreach ($foldersToDelete as $folder) {
       
unlinkRecursive($folder, $verbose);
    }
    foreach (
$filesToDelete as $file) {
        if (
$verbose) {
            echo
"unlink: {$file}\n";
        }
        if (!
unlink($file)) {
            throw new \
RuntimeException("Failed to unlink {$file}: " . var_export(error_get_last(), true));
        }
    }
    if (
$verbose) {
        echo
"rmdir: {$path}\n";
    }
    if (!
rmdir($path)) {
        throw new \
RuntimeException("Failed to rmdir {$path}: " . var_export(error_get_last(), true));
    }
}
?>

Keatran

2 years ago

unlink works the same as the rm command on nix based loses or del command on windows, it will not resolve the file but remove the exact path given even if that path is just a link.

E.G
/var/www/test/index.php = symlink(/home/test/www/index.php)
<?php
unlink
("/var/www/test/index.php");
?>
Will just delete the link, not the original file where as
<?php
unlink
("/home/test/www/index.php");
?>
Will unlink the original file path and break the symlink, and allow the system to overwrite as the filesystem will not know of the file's location anymore.

Alex, the Marrch Ca&#39;at

9 years ago

The best way to delete files by mask is as follows:
<?php
array_walk
(glob('/etc/*'), 'unlink');
?>
Do not use array_map mentioned below - it's purpose is to process values in a given array AND COLLECT data returned by the callback function. So, array_map is slower and uses additional memory compared to array_walk.