My filemanager folder is located here:
localhost/cakeapp/app/webroot/js/tinymce/js/tinymce/plugins/filemanager/
In cakeapp root directory, I have two folders:
uploadedImages (cakeapp/uploadedImages)
UploadedImagesThumbs (cakeapp/uploadedImagesThumbs).
I have this situation in config.php File:
$base_url ="http://localhost";
// path from base_url to base of upload folder (with start and final /)
$upload_dir = '/cakeapp/uploadedImages/';
// relative path from filemanager folder to thumbs folder (with final /)
$thumbs_base_path = '???????/uploadedImagesThumbs';
// relative path from filemanager folder to upload folder (with final /)
$current_path = '???????/uploadedImages/';
How I get the correct relative path for $thumbs_base_path and $current_path from filemanager folder?
You could try something like
function getRelativePath($from, $to) {
$patha = explode('/', $from);
$pathb = explode('/', $to);
$start_point = count(array_intersect($patha,$pathb));
while($start_point--) {
array_shift($patha);
array_shift($pathb);
}
$output = "";
if(($back_count = count($patha))) {
while($back_count--) {
$output .= "../";
}
} else {
$output .= './';
}
return $output . implode('/', $pathb);
}
Check the source.It has many more examples
Related
I made this code below to find the path of file in my locat system, I would like to change this code to locate my-file and return its absolute path knowing that the only thing we remember about this file is that it is saved in a sub-folder /tmp/documents:
/tmp/documents can contain nested sub-folders and my-file can belong to any one of them.
If my-file cannot be found then your program should return NULL
Example:
locateFile() returns /tmp/documents/a/b/c/my-file if my-file is found into the folder /tmp/documents/a/b/c.
<?php
function locatefile(){
$flags = 0;
$pattern = 'my-file';
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir)
{
$files = array_merge($files, locatefile($dir . '/' .basename($pattern), $flags));
}
return $files;
}
// code test do not change it
$folder = '/tmp/documents/' . rand() . '/' . rand();
mkdir($folder, 0700, true);
touch($folder . '/my-file');
echo "EXAMPLE:$folder/my-file\n";
echo "RESULT:" . locatefile();
I need to take backup of code for my Project, but the condition is :-
I generate a Patch folder through SVN, which has files structured in the same way as it is in the main Project folder (the patch has lesser files than the main project). I want to write a code which reads the main project and picks only those files which are present in the patch and saves it in a new backup folder.
The code which i wrote is :-
/*Patch Folder */
$frmFoldr = 'patch_test/';
$basepath = '/var/www/html/TestingBackupCron/';
/*The Folder where files are to be backed up it would use basepath and toFoldr folder name. */
$toFoldr = 'axisdirectcheck/';
/* Read/Copy File from this folder. */
$prodMainFolder = '';
$prodBasePath = '/var/www/html/TestingBackupCron/sijucheckfiles/';
$dir_iterator = new RecursiveDirectoryIterator($basepath . $frmFoldr);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
$readDir = strstr($file->getPathname(), $frmFoldr);
$tofileminpath = str_replace($frmFoldr, "", $readDir);
$fromExPath = $prodBasePath . $tofileminpath;
$toExPath = $basepath . $toFoldr . $tofileminpath;
if (strpos($fromExPath, '.php') || strpos($fromExPath, '.js') || strpos($fromExPath, '.css')) {
if (file_exists($fromExPath)) {
copy($fromExPath, $toExPath);
print_r('Files copied to ' . $toExPath . ".\n");
}else{
print_r($fromExPath." ::: Read path not found.\n");
}
}
}
The above code gives me the error "failed to open stream: No such file or directory". I think copy doesn't create folders. Please help guys.
Found a solution. Below code would create folders first and then on the next loop it would write the copied files onto those folders.
public function run($args) {
/*Patch Folder */
$frmFoldr = 'patch_test/';
$basepath = '/var/www/html/TestingBackupCron/';
/*The Folder where files are to be backed up it would use basepath and toFoldr folder name. */
$toFoldr = 'axisdirectcheck/';
/* Read/Copy File from this folder. */
$prodMainFolder = '';
$prodBasePath = '/var/www/html/TestingBackupCron/sijucheckfiles/';
if (file_exists($basepath . $frmFoldr)) {
$dir_iterator = new RecursiveDirectoryIterator($basepath . $frmFoldr);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
} else{
print_r("Read Directory '$basepath$frmFoldr' not Found. Please check the read directory filename.\n");
exit;
}
$fromDir = array();
$sendToArray = array();
/* this foreach would create folders inside the path ($toFoldr) specified */
foreach ($iterator as $file) {
$readDir = strstr($file->getPathname(), $frmFoldr);
$tofileminpath = str_replace($frmFoldr, "", $readDir);
$fromExPath = $basepath . $readDir;
$toExPath = $basepath . $toFoldr . $tofileminpath;
$sendToArray[] = $tofileminpath;
if (strpos($file, '/.')) {
if (!file_exists($toExPath)) {
$oldmask = umask(0);
mkdir("$toExPath", 0777, true);
umask($oldmask);
}
}
}
/* this foreach would copy files from PROD/UAT and paste inside the path($toExPath) specified */
foreach ($sendToArray as $fPath) {
$fromExPath = $prodBasePath . $fPath;
$toExPath = $basepath . $toFoldr . $fPath;
if (strpos($fromExPath, '.php') || strpos($fromExPath, '.js') || strpos($fromExPath, '.css')) {
if (file_exists($fromExPath)) {
copy($fromExPath, $toExPath);
print_r('Files copied to ' . $toExPath . ".\n");
}else{
print_r($fromExPath." ::: Read path not found.\n");
}
}
}
}
Thanks..
I made a function to get the main folder path in which website is stored. In localhost it works fine.
function get_path()
{
$current=dirname(__FILE__) . '/';
$name=basename(__DIR__);
$from=array($name);
$to=array('');
$result=str_replace($from,$to,$current);
return trim($result, "/\\");
}
But in server it shows error while including files.
include(): Failed opening 'home3/home/public_html/dev/ship\model\main.php' for inclusion (include_path='.:/opt/php54/lib/php')
The file is there in that directory for sure. But its not working.
Try the following
// Define directory separator
define('DS', DIRECTORY_SEPARATOR);
function get_path()
{
$current = dirname(__FILE__) . DS;
$name = basename(__DIR__);
$from = array($name);
$to = array('');
$result = str_replace($from, $to, $current);
return $result;
}
Or you could use:
// define directory separator
define('DS', DIRECTORY_SEPARATOR);
function get_path($withSlash = true)
{
$path = realpath(dirname(__FILE__));
if ($trailingSlash) {
$path .= DS;
}
return $path;
}
You're stripping out the first slash (first character) - plus you're using \ instead of / in the path.
For the creation of paths you should use the PHP-function realpath to manage the slashes: http://php.net/realpath
I am running this code to read a directory (on Apache server):
$mydir = '/media/video/';
$root_dir = $_SERVER["DOCUMENT_ROOT"];
if(strpos($_SERVER['HTTP_HOST'], 'localhost') === false){
$dir = $root_dir . $mydir;
}else{
$dir = $mydir;
}
There have been some server configuration changes recently and now it returns this:
/home2/interact/public_html
Is there a rmore reliable way to always get corrent root path?
I need a public_html path.
// split url path on '/' slash characher and save result in $urlParts(array). check if $urlParts array contains public_html. it means /home2/interact/public_html is root.
$dirPath = dirname ( FILE );
$urlParts = explode('/', $dirPath);
if(in_array('public_html', $urlParts)) {
//do something
}
I have a script which displays the images present on a folder.
E.g. Script path (from HTDOCS):
/admin/global/thisscript.php
E.g. Pictures folder path (from HTDOCS):
/public/test/images/
Now I need to print under each image the path of the file starting from the HTDOCS directory.
E.g. if I have:
$picture1 = /WEB/mysite/htdocs/public/test/images/picture1.png
$picture1name = basename($picture1)
I'd like to print
print "/public/test/images/".$picture1name
having
/public/test/images
in a variable which is automatically update if picture1.png path changes.
If I use
$_SERVER['PHP_SELF']
it returns the path of the file I'm displaing. Also
__FILE__
__DIR__
basename
dirname
are not working for me.
How can I get the path?
Thank you
perhaps $_SERVER['DOCUMENT_ROOT']
edit:
Something like:
$path = "/WEB/mysite/htdocs/public/test/images/picture1.png";
$path = dirname(str_replace($_SERVER['DOCUMENT_ROOT'], '', $path));
Not sure if this could help but could you explode the path result you have and build it back from the HTDOCS entry? something like:
$fullpath = /WEB/mysite/htdocs/public/test/images/picture1.png
$pathbits = explode("/", $fullpath);
$newpath = "";
$foundstart = false;
foreach($pathbits as $key => $value){
if ($value == "htdocs" || $foundstart == true) {
$foundstart = true;
$newpath = $newpath + "/" + $value;
}
}