PHP recursively removing folders - php

Here is function that I'm using for recursively deleting folders and files
function rmdir_recursively($dir) {
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..') continue;
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) {
chmod($dir . DIRECTORY_SEPARATOR . $file, 0777);
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) return false;
};
}
return rmdir($dir);
}
The problem is, when I send some folder inside root, it deletes this folder. But when I send root folder itself like that
rmdir_recursively("./");
It returns bunch of errors like below
PHP Warning: unlink(.//wp/wp-admin/network) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-admin/user) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content/plugins) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content/plugins/akismet) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
What am I missing?

You say you're sending it rmdir_recursively("./");?
But look at the code, you are later calling rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file).
This means that you'll be trying to eliminate files in ".//".
Which is why your error message contains unlink(.//wp/wp-admin/user)
Use rmdir_recursively("."); instead.

Related

ZipArchive ExtractTo() Permission Denied

I'm trying to extract zip files from a folder into a subfolder. The error I'm recieving is:
Warning: ZipArchive::extractTo(XXXX): failed to open stream: Permission denied in /var/www/html/update_alerts2.php on line 97
extracted
Code :
public function extractFiles($inputDir,$outputDir) {
$files_to_extract = $this->getFiles($inputDir);
$zip = new ZipArchive();
foreach ($files_to_extract as $file) {
echo $file;
$res = $zip->open($inputDir . $file);
if ($res) {
$zip->extractTo($outputDir);
$zip->deleteName($inputDir . $file);
$zip->close();
} else {
echo 'failed, code:' . $res;
}
}
}
I've tried setting permissions to 777 but I still get the error. What are the possible ways I can fix this issue (I'm using centos7 and apache) ?

Permission of CSV-file in PHP

My php file has multiple errors. The first one has to do something with permissions. Normally there's nothing wrong with it it, but now I use a csv-file. I think that's the problem. But I don't understand the other warnings.
Which permission do I have to add?
$filename = "menuitems.csv";
$fp = fopen($filename, "r");
while ($data = fgetcsv($fp, filesize($filename), ",")) {
if (trim($data[0]) == '') break;
$href = strtolower(substr($data[1], 0, 7));
if ($href == "http://")
print "<li> " . $data[0] . "</li>\n";
else
print "<li> <a href=\"" . data[1] . "\" >" . $data[0] . "</a></li>\n";
}
fclose($fp);
echo "</ul>\n";
echo "</div>\n";
Warning: fopen(menuitems.csv): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 10
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 11
Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 21
the csv file that was moved into the document root does not have permissions set correctly. You can do this several ways, including changing permission in the file properties dialog (via right click menu) or use the php command:
chmod("/somedir/somefile", 0644);
but installing xampp in its own folder off the root directory instead of Program Files directory will fix this issue in most XAMPP versions.

move file that is uploaded to another pc/ ip address

I just wanna ask if its possible to make directory and store the uploaded file in it to a shared folder from another pc using mkdir and move_uploaded_file. I tried doing it locally it works fine but when i try this code
if($_REQUEST['submit']){
$app_name = $_POST['name'];
$desc = $_POST['description'];
$file_url = "192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"];
$photo_loc = "192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"];
$select = $_POST['select'];
$date = date('m/d/y h:i:s A');
mkdir("192.168.3.34/testupload/files/$app_name/");
mkdir("192.168.3.34/testupload/images/$app_name/");
/* some codes here that is working */
if($file != "" ){
if (file_exists("..192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"]) && file_exists("..192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"])) {
die("File Exists");
}
move_uploaded_file($_FILES["file"]["tmp_name"],
"192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"]);
if($photo_url != "") {
move_uploaded_file($_FILES["photo"]["tmp_name"],
"192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"]);
}
$name = escape($_REQUEST['name']);
$description = escape($_REQUEST['description']);
//$query = "INSERT into files (name, description,file_name,photo_url,date_uploaded) values ('{$name}', '{$description}','{$file}', '{$photo_url}', now() )";
//mysql_query($query) or die("Error in Query" . mysql_error());
//commit();
//redirect("manage.php");
$file_loc = "192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"];
$photo_loc = "192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"];
}
}else{
set_error("File not Found");
//redirect('manage.php');
}
i tried to change the "/" in "\" and still getting this error
Warning: mkdir() [function.mkdir]: No such file or directory in
C:\xampp\htdocs\atts_update\upload.php on line 12
Warning: mkdir() [function.mkdir]: No such file or directory in
C:\xampp\htdocs\atts_update\upload.php on line 13
Warning:
move_uploaded_file(192.168.3.34/testupload/files/1111111111111/sss.txt)
[function.move-uploaded-file]: failed to open stream: No such file or
directory in C:\xampp\htdocs\atts_update\upload.php on line 71
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move 'C:\xampp\tmp\php610B.tmp' to
'192.168.3.34/testupload/files/1111111111111/sss.txt' in
C:\xampp\htdocs\atts_update\upload.php on line 71
Warning:
move_uploaded_file(192.168.3.34/testupload/images/1111111111111/Picture-in-a-picture4aaa.jpg) [function.move-uploaded-file]: failed to open stream: No such file or
directory in C:\xampp\htdocs\atts_update\upload.php on line 76
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move 'C:\xampp\tmp\php611C.tmp' to
'192.168.3.34/testupload/images/1111111111111/Picture-in-a-picture4aaa.jpg' in C:\xampp\htdocs\atts_update\upload.php on line 76

realpath($_SERVER["DOCUMENT_ROOT"]) not working on Windows

I get this error:
Warning: require_once(C:\xampp\htdocs/models/config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\venture\html\admin\index.php on line 3
Here is the code where the error appears:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require_once($root . "/models/config.php");
you need change slash
$root = realpath(str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']) );
require_once($root . "/models/config.php");

Error On Using spl autoload register

This is working fine on init.php
include 'models/m_app.php';
include 'views/v_app.php';
include 'controllers/c_app.php';
but the spl_autoload_register() is not working
spl_autoload_register(function ($class) {
include 'models/' . $class . '.class.php';
include 'views/' . $class . '.class.php';
include 'controllers/' . $class . '.class.php';
});
and I am getting error like
Warning: include(models/Model.class.php): failed to open stream: No such file or directory in C:\wamp\www\App\core\init.php on line 3
Warning: include(): Failed opening 'models/Model.class.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\App\core\init.php on line 3
Warning: include(views/Model.class.php): failed to open stream: No such file or directory in C:\wamp\www\App\core\init.php on line 4
Warning: include(): Failed opening 'views/Model.class.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\App\core\init.php on line 4
Warning: include(controllers/Model.class.php): failed to open stream: No such file or directory in C:\wamp\www\App\core\init.php on line 5
Warning: include(): Failed opening 'controllers/Model.class.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\App\core\init.php on line 5
Can you please let me know why this is happening?
Well, you are using the spl_autoload_register function incorrectly.
An autoload function is getting called, when you are trying to create a class that does not exist.
The correct way would be to name your files just as the class inside them and add an ending like .class.php:
View.class.php
Model.class.php
Controller.class.php
The autoloader:
spl_autoload_register(function ($class) {
set_include_path('views/' . PATH_SEPARATOR . 'controllers/' . PATH_SEPARATOR . 'models/');
include $class . '.class.php';
});
And to test it:
$test1 = new Model();
$test2 = new Controller();
$test3 = new View();

Categories