I have the following function that I am trying to use to delete a full folder but it does not seem to be deleting any ideas or recommendations?
public function submit()
{
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
$data['companyContact'] = $this->quote->getCompanyDetails()->companyContact;
$this->load->view('submit',$data);
$this->quote->removeQuote();
if(is_dir($location.$folderName) === TRUE)
{
$files = array_diff(scandir($location.$folderName), array('.','..'));
foreach($files as $file)
{
Delete(realpath($location.$folderName).'/'. $file);
}
return rmdir($location.$folderName);
}
else if(is_file($location.$folderName) === TRUE)
{
return unlink($location.$folderName);
}
return FALSE;
}
Update:
public function submit()
{
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
$data['companyContact'] = $this->quote->getCompanyDetails()->companyContact;
$this->load->view('submit',$data);
//$this->quote->removeQuote();
$this->removeFolder();
}
private function removeFolder(){
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
foreach(glob($location.$folderName.'/*') as $file)
{
if(is_dir($location.$folderName))
{
rmdir($location.$folderName);
}else{
unlink($location.$folderName);
}
rmdir($location.$folderName);
}
}
You cannot delete a full folder in one call. You should do it recursively:
function rrmdir($dir) {
foreach(glob($dir . '/*') as $file) {
if(is_dir($file))
rrmdir($file);
else
unlink($file);
}
rmdir($dir);
}
Related
I want to delete all files in a folder except files containing:
Specific file names
Specific file extensions
The following code succeeds in the above first point, but not the second point.
function deletefiles()
{
$path = 'files/';
$filesToKeep = array(
$path."example.jpg",
$path."123.png",
$path."*.mkv"
);
$dirList = glob($path.'*');
foreach ($dirList as $file) {
if (! in_array($file, $filesToKeep)) {
if (is_dir($file)) {
rmdir($file);
} else {
unlink($file);
}//END IF
}//END IF
}//END FOREACH LOOP
}
How can I achieve both conditions?
You need to change a bit your function:
<?php
function deletefiles()
{
$path = 'files/';
$filesToKeep = array(
$path . "example.jpg",
$path . "123.png",
);
$extensionsToKeep = array(
"mkv"
);
$dirList = glob($path . '*');
foreach ($dirList as $file) {
if (!in_array($file, $filesToKeep)) {
if (is_dir($file)) {
rmdir($file);
} else {
$fileExtArr = explode('.', $file);
$fileExt = $fileExtArr[count($fileExtArr)-1];
if(!in_array($fileExt, $extensionsToKeep)){
unlink($file);
}
}//END IF
}//END IF
}
}
This function execute successfully but it doesn't deleted any folder.
public function ulink(){
$path='/home/doman/public_html/projectname/';
function Delete($path)
{
if (is_dir($path) === true)
{
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file)
{
Delete(realpath($path) . '/' . $file);
}
return rmdir($path);
}
else if (is_file($path) === true)
{
return unlink($path);
}
return false; }
}
Try this code for remove all folder and sub folder also.
function viewDir($path) {
return is_file($path) ?
#unlink($path) :
array_map(__FUNCTION__, glob($path.'/*')) == #rmdir($path);
}
$dir=$_SERVER["DOCUMENT_ROOT"]."/xxxx/xxxx";
echo $dir;
viewDir($dir);
You can used this function and change $dirvalues for your needs.
it working fine for me..
function delfolder($path) {
$files = array_diff(scandir($path), array('.','..'));
foreach ($files as $file) {
(is_dir("$path/$file")) ? delfolder("$path/$file") : unlink("$path/$file");
}
return rmdir($path);
}
Try like this:
<?php
/**
* Remove the directory and its content (all files and subdirectories).
* #param string $dir the directory name
*/
function rmrf($dir) {
foreach (glob($dir) as $file) {
if (is_dir($file)) {
rmrf("$file/*");
rmdir($file);
} else {
unlink($file);
}
}
}
?>
for more info: http://in3.php.net/glob
I was asked to create a cron job that deletes all files and folders from a folder (recursive)
excluding some file extensions.
I have this code (which I found on the web):
function rrmdir($dir) {
$structure = glob(rtrim($dir, "/").'/*');
if (is_array($structure)) {
foreach($structure as $file) {
if (is_dir($file)) rrmdir($file);
elseif (is_file($file)) unlink($file);
}
}
rmdir($dir);
}
Which will remove ANYTHING from the folder specified,
but as said, I need to add exception to in (all '.php' files should not be remove).
Please assume the following structure for folder:
FOLDER1
FOLDER2
FOLDER3
FILE1.ZIP
FILE2.ZIP
DONOTDELETE1.PHP
DONOTDELETE2.PHP
So, everything should be deleted, except the php files
Can anyone help me with this?
this is modified function try with this. this will delete all file except *.php OR *.PHP
files
function rrmdir($dir) {
$structure = glob(rtrim($dir, "/").'/*');
$rm_dir_flag = true;
if (is_array($structure))
{
foreach($structure as $file)
{
if (is_dir($file))
{
rrmdir($file);
}
else if(is_file($file))
{
$ext = substr($file, -4);
if($ext==".php" || $ext==".PHP")
{
$rm_dir_flag = false;
}
else
{
unlink($file);
}
}
}
}
if($rm_dir_flag)
{
rmdir($dir);
}
}
UPDATE 2:
if you want to protect file with particuar extension you can do this
rrmdir($your_directory, ".php");
//or
rrmdir($your_directory, ".pdf");
//or
rrmdir($your_directory, ".jpeg");
function rrmdir($dir, $protect_extension) {
if(!is_dir($dir))
{
return;
}
$len = strlen($protect_extension)*(-1);
$structure = glob(rtrim($dir, "/").'/*');
$rm_dir_flag = true;
if (is_array($structure))
{
foreach($structure as $file)
{
if (is_dir($file))
{
rrmdir($file, $protect_extension);
}
else if(is_file($file))
{
$ext = substr($file, $len);
if($ext==$protect_extension || $ext==strtoupper($protect_extension))
{
$rm_dir_flag = false;
}
else
{
unlink($file);
}
}
}
}
if($rm_dir_flag)
{
rmdir($dir);
}
}
Can you try this, Added if($ext!='php'){
function rrmdir($dir) {
$structure = glob(rtrim($dir, "/").'/*');
if (is_array($structure)) {
foreach($structure as $file) {
if (is_dir($file)){
rrmdir($file);
}elseif(is_file($file)){
$info = pathinfo($file);
$ext = strtolower($info['extension']);
if($ext!='php'){
unlink($file);
}
}
}
}
rmdir($dir);
}
In my server I have folders and sub-directory
I want to call a flatten methode to a directory to move every files at the same level and remove all empty folders
Here is what i've done so far:
public function flattenDir($dir, $destination=null) {
$files = $this->find($dir . '/*');
foreach ($files as $file) {
if(!$destination){
$destination = $dir . '/' . basename($file);
}
if (!$this->isDirectory($file)) {
$this->move($file, $destination);
}else{
$this->flattenDir($file, $destination);
}
}
foreach ($files as $file) {
$localdir = dirname($file);
if ($this->isDirectory($localdir) && $this->isEmptyDirectory($localdir) && ($localdir != $dir)) {
$this->remove($localdir);
}
}
}
public function find($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
$files = array_merge($files, $this->find($dir . '/' . basename($pattern), $flags));
}
return $files;
}
This code dont show any error on run, but the resukt is not as expected.
For exemple if I have /folder1/folder2/file I want to have /folder2/file as a result but here the folders still like they where ...
I finally make my code works, I post it here in case it help someone
I simplified it to make it more efficient. By the way this function is part of a filemanager classe I made, so I use function of my own class, but you can simply replace $this->move($file, $destination); by move($file, $destination);
public function flattenDir($dir, $destination = null) {
$files = $this->find($dir . '/*');
foreach ($files as $file) {
$localdir = dirname($file);
if (!$this->isDirectory($file)) {
$destination = $dir . '/' . basename($file);
$this->move($file, $destination);
}
if ($this->isDirectory($localdir) && $this->isEmptyDirectory($localdir) && ($localdir != $dir)) {
$this->remove($localdir);
}
}
}
Hi
i am trying to build simple directory browser to browse folders and sub-folders uing php RecursiveDirectoryIterator .. i need help of how to create this. i have started with the following code.
$dir = dirname(__FILE__); //path of the directory to read
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if (!$file->isFile()) { //create hyperlink if this is a folder
echo "" . $file->getFilename() . "\";
}else{ //do not link if this is a file
$file->getFilename()
}
}
Allow me to code that for you....
<?php
$root = __DIR__;
function is_in_dir($file, $directory, $recursive = true, $limit = 1000) {
$directory = realpath($directory);
$parent = realpath($file);
$i = 0;
while ($parent) {
if ($directory == $parent) return true;
if ($parent == dirname($parent) || !$recursive) break;
$parent = dirname($parent);
}
return false;
}
$path = null;
if (isset($_GET['file'])) {
$path = $_GET['file'];
if (!is_in_dir($_GET['file'], $root)) {
$path = null;
} else {
$path = '/'.$path;
}
}
if (is_file($root.$path)) {
readfile($root.$path);
return;
}
if ($path) echo '..<br />';
foreach (glob($root.$path.'/*') as $file) {
$file = realpath($file);
$link = substr($file, strlen($root) + 1);
echo ''.basename($file).'<br />';
}