I try to delete everything in folder (including subfolders) but:
Warning: unlink(./../kaj-content/theme/one-four): Permission denied in
C:\wamp\www\kaj\kaj-admin\includes\incAppearance.php on line 36
this is my code:
$themeDirectory = './../kaj-content/theme';
$dir = $themeDirectory . '/' . $themeName;
array_map('unlink', glob($dir));
how can I change my code ?
Other code didn't work, like:
function rrmdir($dir) {
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file)
if ($file != "." && $file != "..")
rrmdir("$dir/$file");
rmdir($dir);
} else if (file_exists($dir))
unlink($dir);
}
Your php code is running as a particular user, maybe apache.
Your error means that php does not have the correct permissions for directory one-four.
Check the permissions on that directory. Grant write permission for that directory to the user that php is using. Then, your code will be able to delete the file.
Related
I get this error message when I try to delete a directory:
"Unable to delete the file" using PHP Codeigniter's delete_dir('/path/to/my/dir/'). No additional info provided.
There is no problem with the ftp conn (works with everything else), no problem with the file permission (delete_file() works jsut fine)..no problem with the code, the path is also valid. I'm all out of ideas and open for suggestions.
You can use unlink() to remove php directory.
function removedir($dir1) {
if (is_dir($dir1)) {
//scan
$files = scandir($dir1);
//loop through all the files
foreach ($files as $file) {
if ($file != "." && $file != "..") {
if (filetype($dir1."/".$file) == "dir1")
rrmdir($dir1."/".$file);
else
unlink($dir1."/".$file);
}
}
reset($files);
rmdir($dir);
}
}
I have a server running PHP Version 5.4.16 and am trying to use scandir to list files within a directory. I am having a hard time figuring out what the issue is. I've tried both ../store/dir_to_scan and /root/store/dir_to_scan. I've also tried using both glob and scandir as you can see below both to no avail. If I remove the dir_to_scan directory it will list the directories inside of /root/store which is what I find most puzzling of all. I've also chmod'd the folders and files to 777 just to make sure it wasn't a permissions issue. I receive an error of "Array ( [type] => 2 [message] => Invalid argument supplied for foreach() [file] => /root/html/test.php [line] => 5 )" also upon running with correct directory setup.
Thanks for any help.
Directory Setup
/root/html //where php script is run
/root/store/dir_to_scan //files I need to list
PHP Script
<?
#$files = glob('../store/dir_to_scan/*', GLOB_BRACE);
$files = scandir("/root/store/dir_to_scan/");
foreach($files as $file) {
//do work here
if ($file === '.' || $file === '..') {
continue;
}
echo $file . "<br>";
}
print_r(error_get_last());
?>
this might be silly but try supplying a trailing slash at the end of:
/root/store/dir_to_scan ->
$files = scandir("/root/store/dir_to_scan/");
this should to solve your problem
$carpeta= "/root/store/dir_to_scan";
if(is_dir($carpeta)){
if($dir = opendir($carpeta)){
while(($file = readdir($dir)) !== false){
if($file != '.' && $file != '..' && $file != '.htaccess'){
echo $file; //or save file name in an array..
// $array_file[] = $file;
}
}
closedir($dir);
}
}
I have a Video folder on my server which has 755 permission. The problem is: when someone goes to upload video file, it can't be upload into that folder because of permission error.
If I change the permission to 777, then Video can be uploaded. But I don't want to allow the folder permission to 777 for security reason.
Is there any way in PHP to temporary change the permission to 777 while uploading video?
PHP provides a function, chmod() for the task.
Attempts to change the mode of the specified file to that given in mode.
You can put it in an if statement, and if it returns false, you can skip the upload file part.
The usage will be like
if( chmod($path, 0777) ) {
// more code
chmod($path, 0755);
}
else
echo "Couldn't do it.";
As described in the chmod function manual, the $mode must be in octal format - with leading zero, i.e chmod($path, 0777)
There is a way (PHP provides chmod function) but since PHP is not the owner of the folder, you won't be able to change the permission. And I think you are solving the wrong problem. Add webserver and PHP in the same group and give 775 to the folder.
You have to initialize the config for the upload, like this:
$config['remove_spaces'] = FALSE;
$config['upload_path'] = $path;
$this->upload->initialize($config);
$this->load->library('upload', $config);
You can use chmod() function.
For more information, try here
Warning: You cannot undo the file permissions that are changed by the script below. Proceed with extreme caution.
Important: this code should only be used if you remember to delete it immediately after use. As above, its use may put your site into an insecure state.
//replace dirname(__FILE__) with desired folder.
file_fix_directory(dirname(__FILE__));
function file_fix_directory($dir, $nomask = array('.', '..')) {
if (is_dir($dir)) {
// Try to make each directory world writable.
if (#chmod($dir, 0777)) {
echo "Made writable: " . $dir . "";
}
}
if (is_dir($dir) && $handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $nomask) && $file[0] != '.') {
if (is_dir("$dir/$file")) {
// Recurse into subdirectories
file_fix_directory("$dir/$file", $nomask);
}
else {
$filename = "$dir/$file";
// Try to make each file world writable.
if (#chmod($filename, 0666)) {
echo "Made writable: " . $filename . "";
}
}
}
}
closedir($handle);
}
}
or you can use terminal for this
chmod -R 755 public_html/test
I once used this wrong PHP script which created a 340 mode directories:
<?php
$uname = "secret";
mkdir("/home/u251526215/public_html/user/profile/".$uname."", 755);
?>
The script above creates a 340 CHMOD directories. I've repaired the "755" to "0755" and it is now working perfectly. But for now, how can I delete the 340 directories which were already created? I have tried to delete them using FTP manager, but it kept saying error. I've tried to use rmdir() but it says directory not empty but it is completely empty!
Updated: All action to the directory; rename, move, copy, change permission and open are error returned
Maybe there is a hidden file. I found this function to delete a directory with all contents:
function delete_directory($dirname) {
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_directory($dirname.'/'.$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}
Source http://www.ozzu.com/programming-forum/php-delete-directory-folder-t47492.html
I have main folder named "gallery". In this folder there are some sub folders like "animal", "tree", etc. folder "animal" contains some pictures and so on. suppose i want to delete "animal" folder with all pictures in it, how can do this?
i tried-
rmdirr($_SERVER['DOCUMENT_ROOT']."admin/gallery/animal");
but in server, it deleted the whole "gallery" folder with all in it. please tell me what i am doing wrong and also give me a solution. thanks in advance.
If there are no subfolders you can use glob() to empty the directory before using rmdir():
foreach( glob( "/path/to/dir/*.*" ) as $filename ) {
unlink( $filename );
}
rmdir( "/path/to/dir" );
By the way, rmdir() shouldn't delete any files. It just fails if the directory isn't empty. You might want to make sure there's nothing else in your code that causes the parent directory to be wiped out.
Just delete the directory recursively with a helper function:
rrmdir($_SERVER['DOCUMENT_ROOT']."admin/gallery/animal");
function rrmdir($path)
{
return is_file($path)
? #unlink($path)
: array_map('rrmdir', glob($path.'/*')) == #rmdir($path)
;
}
When removing a desired directory, you must first remove the files within that directory. Once that is complete, then you may remove the directory -- assuming you have the proper permissions.
Included below is code that removes the files in the directory and will then remove the specified directory itself:
<?php
$dirname = "animal";
function destroy($dir) {
$mydir = opendir($dir);
while(false !== ($file = readdir($mydir))) {
if($file != "." && $file != "..") {
chmod($dir.$file, 0777);
if(is_dir($dir.$file)) {
chdir('.');
destroy($dir.$file.'/');
rmdir($dir.$file) or DIE("Unable to delete $dir$file");
}else{
unlink($dir.$file) or DIE("Unable to delete $dir$file");
}
}
}
closedir($mydir);
}
destroyDir($dirname);
?>