Move Zpanel Default Backups Directory or Opendir Backups directory - php

I'm working on a script which will upload all the zpanel backups to my Amazon S3 account. It works if I don't use the zpanel backups and create and upload backups created by me using CronJob.
The reason why I cannot upload the zpanel backup is I cannot figure out a way to reach the "backups" directory using php opendir function.
I know the absolute path of the backups folder though. And as it seems, this absolute path won't work with opendir()
I've used : opendir('var/zpanel/hostdata/my_username/backups/')
And it won't work. Now if I want to use the relative path, I cannot reach there either.
So, is there a way that I can move the zPanel Backups directory somewhere inside the public_html folder? Or, a web URL that can reach the backups folder? Ajaxplorer can reach there, why I cannot?
If none is possible, I'll greatly appreciate if someone can teach me a way to create backups exactly like zPanel (all DB + everything inside public_html folder).
The code looks like this
require_once('S3.php');
// Enter your amazon s3 creadentials
$s3 = new S3('xxxxx', 'xxxxx');
if ($handle = opendir('var/zpanel/hostdata/my_username/backups/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
//echo "okay";
echo $file; exit;
if ($s3->putObjectFile("backups/$file", "my_basket", "$file", S3::ACL_PUBLIC_READ)) {
echo "<strong>We successfully uploaded your file.<br /></strong>";
//this will delete the file from your server after upload
//if (file_exists($baseurl . '/' . $file)) { unlink ($baseurl . '/' . $file); }
}else{
echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
}else{
echo "No file found here ".$root;
}
}
closedir($handle);
}
Thanks a lot in advance!

You have a typo in your code. If you want to use an absolute path with opendir, make sure it starts with a slash (/). So the correct version would be:
opendir('/var/zpanel/hostdata/my_username/backups/')
If you still can't get this to work, verify that the path you pass to opendir is indeed a directory and also verify the permissions on that directory to make sure your PHP process can read from it.
From the PHP doc,
If path is not a valid directory or the directory can not be opened
due to permission restrictions or filesystem errors, opendir() returns
FALSE ...
Also, if you use a any open-basedir restrictions in your php.ini or in your code, make sure the path you pass to opendir is not blocked by those restrictions.

Related

Change specific extension with * as name keeping name using php

So some reason server changes random (?) .php files to .ph.
Need to rename any .ph file to .php
Tried about every rename and rename extension code I found on stackflow.
Nothing has worked so far.
No root access (shared)
working directory will be /
You can try below code :(Tested in localhost)
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
unlink('test.ph');
?>
If you are looking to rename all the .ph files in the current directory to .php, you can try this:
<?php
$old_extension = '.ph';
$new_extension = '.php';
$files = glob("*$old_extension");
foreach($files as $file){
$filename = pathinfo($file,PATHINFO_FILENAME);
rename($file,$filename . $new_extension);
}
?>
A word of caution: the script will rename the existing files in the current directory without warning.
I highly advise running the above script in a development environment or within a Test directory beforehand so you know what to expect.
Best-

CodeIgniter download a file to windows via FTP

In my website I have the following 2 functions. The first one takes the FTP connection and creates a directory in the specified path. server.com/Host_path/DirectoryName for example. This piece of code works perfectly fine. I have an upload function that uploads file test.txt to the folder DirectoryName and the second function is supposed to download that file to my local Windows machine. However i get the error message Unable to download the specified file. Please check your path. With no indication as to which path is wrong.
$path = 'server.com/' . $result['path'];
$this->ftp->mkdir($path);
When I echo the remote and local paths I get remote = server.com/Host_path/DirectoryName/test.txt and local = C:\Users\Owner\Desktop\test.txt . Based on the fact that the above function works but the below function does not, I believe that the Windows path is wrong but it could be a difference in functions.
$remotePath = 'server.com/' . $result['path'];
$localPath = 'C:\\Users\\Owner\\Desktop\\test.txt';
$this->ftp->download($localPath, $remotePath, 'auto');`
Please Help!
For remotePath, use absolute path starting with "/"

Move uploaded file to root directory

I'm making a content management system for a website I built. I want the system to be discrete, so I made it exist in only one PHP file, called '_admin.php'. All the content displayed in this file comes from includes that I store in a sub-folder called 'admin' (out of the way).
The photos used on the website are stored in an 'assets' folder that also sits in the root dir. The admin page has direct access to the assets folder, as it is also in the root. But the upload file script sits a few directories into the 'admin' folder and I want the uploaded files to be stored in the assets folder.
The move_uploaded_file() method takes the destination path for the file, but it requires a direct path. I try using $_SERVER['DOCUMENT_ROOT'], but the resulting directory doesn't seem to have any of my files. If I use getcwd() in a doc in the root, it returns the actual file structure that I can use. The same if I echo out __FILE__. But I've experimented with this SERVER constant a lot and I can't locate my website with it.
Since the script that uploads the images is called as a form action, I can't pass the root directory as a variable.
Not really sure what I'm doing wrong, anyone have any ideas?
Thanks
edit **
//See if Files array contains new files
if (!empty($_FILES['file'])){
foreach($_FILES['file']['name'] as $key => $name){
$error = $_FILES['file']['error'][$key];
$temp_name = $_FILES['file']['tmp_name'][$key];
$dir = getcwd();
$move_file = move_uploaded_file($temp_name, "$dir/temp/$name");
if (($error == 0) && ($move_file)){
$uploaded[] = $name;
}else{
die($error);
}
}
echo __FILE__;
echo "<br/>";
echo __DIR__;
echo "<br/>";
echo $_SERVER['DOCUMENT_ROOT'];
exit();
}
The upload script I'm currently using. This script works fine because I'm storing the images in the same directory as the script. I just don't know how to store them in my root.
I would specify the full absolute file path if you can. I set this via define() in a config file for my CMS. On install you figure out what that path is and set it.
define("BASEFILEPATH", "/home/.../[webroot]"); // The base file path for the website
You may be looking for something more general, but you could have some sort of install script where the user can enter basic info into a form, such as username, pwd, etc. and you could have them enter this path as well.

permission denied - php unlink

I have two files:
b.php and test.txt
<?php
$b = "test.txt";
unlink($b);
?>
and the error is: Warning: unlink(test.txt) [function.unlink]: Permission denied
why? b.php and test.txt is 777 and the same group/login
if I set 777 on the parent directory I can execute unlink but i have to set 777 and back to 755?
You (as in the process that runs b.php, either you through CLI or a webserver) need write access to the directory in which the files are located. You are updating the directory content, so access to the file is not enough.
Note that if you use the PHP chmod() function to set the mode of a file or folder to 777 you should use 0777 to make sure the number is correctly interpreted as an octal number.
You'll first require to close the file using fclose($handle); it's not deleting because the file is in use. So first close the file and then try.
in addition to all the answers that other friends have , if somebody who is looking this post is looking for a way to delete a "Folder" not a "file" , should take care that Folders must delete by php rmdir() function and if u want to delete a "Folder" by unlink() , u will encounter with a wrong Warning message that says "permission denied"
however u can make folders & files by mkdir() but the way u delete folders (rmdir()) is different from the way you delete files(unlink())
eventually as a fact:
in many programming languages, any permission related error may not
directly means an actual permission issue
for example, if you want to readSync a file that doesn't exist with node fs module you will encounter a wrong EPERM error
// Path relative to where the php file is or absolute server path
chdir($FilePath); // Comment this out if you are on the same folder
chown($FileName,465); //Insert an Invalid UserId to set to Nobody Owner; for instance 465
$do = unlink($FileName);
if($do=="1"){
echo "The file was deleted successfully.";
} else { echo "There was an error trying to delete the file."; }
Try this. Hope it helps.
The file permission is okay (0777) but i think your on the shared server, so to delete your file correctly use;
1. create a correct path to your file
// delete from folder
$filename = 'test.txt';
$ifile = '/newy/made/link/uploads/'. $filename; // this is the actual path to the file you want to delete.
unlink($_SERVER['DOCUMENT_ROOT'] .$ifile); // use server document root
// your file will be removed from the folder
That small code will do the magic and remove any selected file you want from any folder provided the actual file path is collect.
In Windows and before PHP version 7.3.0, check that your file has been closed before unlinking it,
as said in https://www.php.net/manual/en/function.unlink.php :
On Windows, it is now possible to unlink() files with handles in use, while formerly that would fail. However, it is still not possible to re-create the unlinked file, until all handles to it have been closed.
As an exemple :
$fullFilePath = 'C:\Users\MyUserName\www\myApp\public\test.txt';
$handle = fopen($fullFilePath , 'w+');
fopen($filePath, 'w+');
fputs($handle, 'Some text in the file');
fclose($handle);
unlink(realpath($insertedLinesFilePath));

PHP Directory handling

My project files are located on remote server in the folder. I access a file in this folder in this way:
http://www.example.com/searchtest.html
This opens a page with Input Box where user types keywords to search. The search script is a .php file located in the root itself. The script has to search for .html files with the name similar to the keywords entered. These .html files are also located in the same root folder where all .php files reside.
My application is running well when I work on local machine and is able to search well, but when I hosted it on my site, it gives error:
Warning: file_get_contents(//.rnd) [function.file-get-contents]: failed to open stream: Permission denied in /home/myServer/public_html/example/search1.php on line 40
.rnd
It is giving error on a line where I am trying to access files in my directory. Below is my code snippet. And yes, I am the administrator and I have all privileges on my site.
$matchingFiles = array();
$dirName = "\\";
$dh = opendir($dirName);
while( ($file = readdir($dh)) !== false)
{
$fullPath = $dirName . "/" . $file;
if(is_dir($fullPath)) continue; // Skip directories
similar_text($lcSearch,strtolower($file),$percentSimilar);
if($percentSimilar >= $percentMatch)
{
$matchingFiles[] = $fullPath;
}
}
It gives error probably in the "opendir" function.
I also want to know whether the $dirName holds correct path or not. It must hold the same path from where the correct script is run.
Assuming you did not change the $dirName assignment to omit server detail on a public posting, you should be setting that to the path to search. Since your use is pretty static, I'd suggest using the full path to the directory to open, e.g.
$dirName = '/home/myServer/public_html';
And of course make sure the permissions to all files it will search are compatible with the user your server is running as.

Categories