php教程

User Contributed Notes

muhammad dot begawala at gmail dot com 29-Sep-2018 05:02
Handling "Resource Unavailable" error by unlink() as Exception using try catch

Even is_file() or file_exists() will check for file is exists or not, there are chances that file is being used by some applications that will prevent deletion and unlink() will display "Resource Unavailable" error.

So after trying many methods like: `is_resource()`, `is_writable()`, `stream_get_meta_data()`...etc, I reached the only best way to handle error while *"deleting"* a file that is either **not exists** or **is exists but being used by some application**

function delete_file($pFilename)
{
    if ( file_exists($pFilename) ) {
        //    Added by muhammad.begawala
        //    '@' will stop displaying "Resource Unavailable" error because of file is open some where.
        //    'unlink($pFilename) !== true' will check if file is deleted successfully.
        //  Throwing exception so that we can handle error easily instead of displaying to users.
        if( @unlink($pFilename) !== true )
            throw new Exception('Could not delete file: ' . $pFilename . ' Please close all applications that are using it.');
    }   
    return true;
}

/* === USAGE === */

try {
    if( delete_file('hello_world.xlsx') === true )
        echo 'File Deleted';
}
catch (Exception $e) {
    echo $e->getMessage(); // will print Exception message defined above.
}
pedro dot goca at hotmail dot com 10-Jul-2017 08:26
A mi solamente me funcionó de la siguiente manera:

<?php

unlink
("muestras.pdf");

//Le tuve que poner comillas dobbles

 
?>
llmll at gmx dot de 11-Apr-2015 09:53
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.
softontherocks at gmail dot com 29-Oct-2014 06:13
Here you have a function to delete a directory and subdirectories recursively:

function deleteDirectory($dir){
    $result = false;
    if ($handle = opendir("$dir")){
        $result = true;
        while ((($file=readdir($handle))!==false) && ($result)){
            if ($file!='.' && $file!='..'){
                if (is_dir("$dir/$file")){
                    $result = deleteDirectory("$dir/$file");
                } else {
                    $result = unlink("$dir/$fich");
                }
            }
        }
        closedir($handle);
        if ($result){
            $result = rmdir($dir);
        }
    }
    return $result;
}

The source is found in http://softontherocks.blogspot.com/2014/09/eliminar-un-directorio-completo-con-php.html
Eric 11-Aug-2014 02:17
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.
deen804 at gmail dot com 10-Dec-2013 06:14
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 .
james at dunmore dot me dot uk 24-Oct-2013 02:52
I know it is obvious, but.... if you have nested symlinks (for example, we archive into yyyy-mm for backups, and symbolic link to make our directory structure look flat - don't ask), unlink will only delete the top level symlink (as expected).

If you want to delete the actual file, you'll need to use readlink. If you want to delete all of the files and the symlinks, you'll need to recurse up and down to achieve that.
Alex, the Marrch Ca&#39;at 26-Jun-2013 04:22
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.
anagai at yahoo dot com 06-Sep-2012 07:47
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 31-Dec-2010 01:28
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...
federico at poisonfx dot com 06-Dec-2010 02:55
Here the simplest way to delete files with mask

<?php
   $mask
= "*.jpg"
  
array_map( "unlink", glob( $mask ) );
?>
lpatrick 25-Oct-2010 02:08
unlink can fail after using ODBC commands on the file (on Windows).

Neither <?php odbc_free_result($result); ?> nor <?php odbc_close($conn); unset($conn); ?> did the trick.

Only <?php odbc_close_all(); ?> released the file such that it could be deleted afterwards ...
Anonymous 01-Nov-2008 06:13
To unlink, the web server user must have write permissions to the directory.

Conversely, if a user has write permissions to a directory, it may delete files from that directory regardless of who owns them...
gotdalife at gmail dot com 25-Sep-2008 07:04
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   
?>
PD 06-Aug-2008 05:07
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);
?>
ayor ATTTTT ayor.biz 20-Dec-2007 06:02
ggarciaa's post above has already one small error, closedir has to be used even if $DeleteMe is false

<?php
// ggarciaa at gmail dot com (04-July-2007 01:57)
// I needed to empty a directory, but keeping it
// so I slightly modified the contribution from
// stefano at takys dot it (28-Dec-2005 11:57)
// A short but powerfull recursive function
// that works also if the dirs contain hidden files
//
// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone

function SureRemoveDir($dir, $DeleteMe) {
    if(!
$dh = @opendir($dir)) return;
    while (
false !== ($obj = readdir($dh))) {
        if(
$obj=='.' || $obj=='..') continue;
        if (!@
unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
    }

   
closedir($dh);
    if (
$DeleteMe){
        @
rmdir($dir);
    }
}

//SureRemoveDir('EmptyMe', false);
//SureRemoveDir('RemoveMe', true);

?>
rahulnvaidya at gmail dot com 03-Aug-2007 02:36
ggarciaa's post above has one small error, it will ignore file and directory strings that are evaluated as false (ie. "0")

Fixed code is below (false !==)

<?php
// ggarciaa at gmail dot com (04-July-2007 01:57)
// I needed to empty a directory, but keeping it
// so I slightly modified the contribution from
// stefano at takys dot it (28-Dec-2005 11:57)
// A short but powerfull recursive function
// that works also if the dirs contain hidden files
//
// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone

function SureRemoveDir($dir, $DeleteMe) {
    if(!
$dh = @opendir($dir)) return;
    while (
false !== ($obj = readdir($dh))) {
        if(
$obj=='.' || $obj=='..') continue;
        if (!@
unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
    }
    if (
$DeleteMe){
       
closedir($dh);
        @
rmdir($dir);
    }
}

//SureRemoveDir('EmptyMe', false);
//SureRemoveDir('RemoveMe', true);

?>
jchase at solidmark dot com 20-May-2005 10:05
[Editor's note: A suggestion for a work-around was submitted by argistof at gmail dot com: You can use the recursive option (see man chmod) when chmodding, for instance 'chmod 777 directory/ -R'. Be aware though, this will change the permissions of all files and folders in the diectory.]

Just a note which you probably all know, but I didn't, and it might save another poor sap some unnecessary time:

I was doing unlink() and fopen() on a file and got a permission denied error, even after chmoding the file to 0777. 

The folder that contains the file must ALSO have write permission.  Took a headache to find this out. 

Hope this helps someone :)
ashley at semantic dot org 02-Apr-2005 02:50
Actually you should use "@unlink" rather than testing with file_exists. The former is atomic, whereas the latter can break if you can't guarantee only one process will try to delete a given file at a time.
dagski_AT_gmail_DOT_com 06-Feb-2005 10:19
before you could unlink a file created which uses a handle e.g.,

$handle = sqlite('temp.db');

unset($handle); first befofe
unlink($handle);

to avoide permission denied error.
chris at vibenewmedia dot com 14-Sep-2004 10:54
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 .........

<?php

foreach (glob("*.jpg") as $filename) {
   echo
"$filename size " . filesize($filename) . "\n";
  
unlink($filename);
}

?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788