Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php delete a single file in directory

Tags:

php

I've got the php list directory script from this link http://www.gaijin.at/en/scrphpfilelist.php. How do I delete a single file from the directoy? I tried unlink, but it deleted all the files from that directory. this the short code what i got from the link!

while ($file = readdir ($hDir)) {
if ( ($file != '.') && ($file != '..') && (substr($file, 0, 1) != '.') &&
     (strtolower($file) != strtolower(substr($DescFile, -(strlen($file))))) &&
     (!IsFileExcluded($Directory.'/'.$file))
   ) {

  array_push($FilesArray, array('FileName' => $file,
                                'IsDir' => is_dir($Directory.'/'.$file),
                                'FileSize' => filesize($Directory.'/'.$file),
                                'FileTime' => filemtime($Directory.'/'.$file)
                                ));
}
}
$BaseDir = '../_cron/backup';
$Directory = $BaseDir;

foreach($FilesArray as $file) {
  $FileLink = $Directory.'/'.$file['FileName'];
  if ($OpenFileInNewTab) $LinkTarget = ' target="_blank"'; else $LinkTarget = '';
    echo '<a href="'.$FileLink.'">'.$FileName.'</a>';
    echo '<a href="'.unlink($FileLink).'"><img src="images/icons/delete.gif"></a></td>';
  }
}

the list directory folder call : backup.
in the unlink($FileLink), when i hover the link has change to another folder to admin folder?

like image 280
tonoslfx Avatar asked Mar 17 '11 11:03

tonoslfx


People also ask

How do I delete a file in PHP?

To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.

Which function is used in PHP to delete a file?

PHP | unlink() Function The unlink() function is an inbuilt function in PHP which is used to delete files.

How can I delete all files in a directory in PHP?

If you want to delete everything from folder (including subfolders) use this combination of array_map , unlink and glob : array_map( 'unlink', array_filter((array) glob("path/to/temp/*") ) );


1 Answers

http://php.net/manual/en/function.unlink.php

Unlink can safely remove a single file; just make sure the file you are removing it actually a file and not a directory ('.' or '..')

if (is_file($filepath))
  {
    unlink($filepath);
  }
like image 162
Tramov Avatar answered Oct 12 '22 02:10

Tramov