How to Delete a File via PHP

How to delete a file via PHP?

The following should help

  • realpath — Returns canonicalized absolute pathname
  • is_writable — Tells whether the filename is writable
  • unlink — Deletes a file

Run your filepath through realpath, then check if the returned path is writable and if so, unlink it.

how to delete a file from a directory using php

unlink() will delete the file off your server

rmdir() will delete a directory off your server

Note: Once it's gone, it's gone.

How to delete file in PHP

I think your question isn't specific, this code must clear all files in the directory 'files'.

But there are some errors in that code I think, and here is the right code:

        $files= array();
$dir = dir('files');
while (($file = $dir->read()) !== false) { // You must supply a condition to avoid infinite looping
if ($file != '.' && $file != '..') {
$files[] = $file; // In this array you push the valid files in the provided directory, which are not (. , ..)
}
unlink('files/'.$file); // This must remove the file in the queue
}

And finally make sure that you provided the right path to dir().

how to delete a file in php from a directory

See the error :

unlink(C:/wamp/www/jedda/wp-content/uploads/)

You are trying to delete the folder "uploads" , not the file . Unlink can delete files only NOT folder . Make sure your argument to unlink() is a valid file .

php delete a single file in directory

unlink('path_to_filename'); will delete one file at a time.

If your whole files from directory is gone means you listed all files and deleted one by one in a loop.

Well you cannot de delete in the same page. You have to do with other page. create a page called deletepage.php which will contain script to delete and link to that page with 'file' as parameter.

foreach($FilesArray as $file)
{
$FileLink = $Directory.'/'.$file['FileName'];

if($OpenFileInNewTab) $LinkTarget = ' target="_blank"';
else $LinkTarget = '';

echo '<a href="'.$FileLink.'">'.$FileName.'</a>';
echo '<a href="deletepage.php?file='.$fileName.'"><img src="images/icons/delete.gif"></a></td>';
}

On the deletepage.php

//and also consider to check if the file exists as with the other guy suggested.
$filename = $_GET['file']; //get the filename
unlink('DIRNAME'.DIRECTORY_SEPARATOR.$filename); //delete it
header('location: backto prev'); //redirect back to the other page

If you don't want to navigate, then use ajax to make elegant.

Delete directory with files in it?

There are at least two options available nowadays.

  1. Before deleting the folder, delete all its files and folders (and this means recursion!). Here is an example:

    public static function deleteDir($dirPath) {
    if (! is_dir($dirPath)) {
    throw new InvalidArgumentException("$dirPath must be a directory");
    }
    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
    $dirPath .= '/';
    }
    $files = glob($dirPath . '*', GLOB_MARK);
    foreach ($files as $file) {
    if (is_dir($file)) {
    self::deleteDir($file);
    } else {
    unlink($file);
    }
    }
    rmdir($dirPath);
    }
  2. And if you are using 5.2+ you can use a RecursiveIterator to do it without implementing the recursion yourself:

    $dir = 'samples' . DIRECTORY_SEPARATOR . 'sampledirtree';
    $it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
    $files = new RecursiveIteratorIterator($it,
    RecursiveIteratorIterator::CHILD_FIRST);
    foreach($files as $file) {
    if ($file->isDir()){
    rmdir($file->getRealPath());
    } else {
    unlink($file->getRealPath());
    }
    }
    rmdir($dir);


Related Topics



Leave a reply



Submit