Right so i'm making a configuration class that will use an array of different file types in 4 main locations. Now I want to make it so that the configuration class will search these locations in order at the moment i'm using the following
if (file_exists(ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file)) {
$this->filePath = ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(ROOT . DS . "Application/Config/$file")) {
$this->filePath = ROOT . DS . "Application/Config/$file";
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file)) {
$this->filePath = CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . "Config/$file")) {
$this->filePath = CARBON_PATH . "Config/$file";
echo $this->filePath;
} else {
throw new \Exception("Unable to locate: $file, Please check it exists");
}
}
}
}
pretty messy and not very flexible.
What I want to be able to do is search the locations in the same order BY FILE NAME ONLY after finding the first match It would then return the file with the extension for the configuration class to use the correct method to parse into a php array and so on.
What is the best way to search these locations for a file name
Example
Say we want a database configuration file as you can see there are 2
ConfigLocation1/Dev/
/file.php
/database.json
ConfigLocation1/
/database.ini
/anotherfile.json
I would want to use the function like so
config::findFile('database');
and it return
$result = ConfigLocation1/Dev/database.json
but if it wasnt found here then then
$result = ConfigLocation1/database.ini
Not very good at explaining things so hope the example helps
As you mentioned you need to check for file in 4 locations, so instead of if conditions, create an array of directories and loop through.
and you can use glob, to find a file irrespective of extension. see my example below:-
//Make a array of directory where you want to look for files.
$dirs = array(
ROOT . DS . 'Application/Config/' . APP_ENV . DS,
CARBON_PATH . 'Config' . DS . APP_ENV . DS
);
function findFiles($directory, $filename){
$match = array();
foreach ($directory => $dir) {
$files = glob($dir.$filename);
foreach ($files as $file) {
$match[] = $file;
}
}
return $match;
}
// to find database
$results = findFiles($dirs, 'database.*');
Related
So I am trying to set up path for my pdf files that are stored in a folder structure. The file that has to be selected depends on the user input.
I want to set up first the absolute path and then a folder pattern.
The folder where I have stored my files is:
C:\Apache24\htdocs\archivedb\Tourenfahrer\2017\5
The last three folders changes as per user input.
I have setup my root directory in my config.php like this:
define( 'ROOT_DIR', dirname(__FILE__) );
Now in my php file I am recieving my variables, which will be my folder names to search for the files.
<?php
require_once 'conf/config.php';
if (!empty($_REQUEST['magName'] && $_REQUEST['year'] && $_REQUEST['issue']
)) {
$magazineName = $_REQUEST['magName'];
$year = $_REQUEST['year'] ;
$issue = $_REQUEST['issue'] ;
}
Now Please tell how to access the respective folder using these variables?
and how can i set up a pattern with this using glob(); ?
Please try this
define('ROOT_DIR', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
if (!empty($_REQUEST['magName'] && $_REQUEST['year'] && $_REQUEST['issue'])) {
$magazineName = $_REQUEST['magName'];
$year = $_REQUEST['year'];
$issue = $_REQUEST['issue'];
$dir = $magazineName . DS . $year . DS . $issue;
echo "<h3>Use scandir:</h3>";
$files = scandir(ROOT_DIR . DS . $dir);
foreach ($files as $file) {
echo basename($file) . "<br>";
}
echo "<h3>Use glob:</h3>";
foreach (glob($dir . DS . '*') as $filename) {
echo basename($filename) . "<br>";
}
}
I am trying to create a zip file with folders inside it.
Now the code is :
{
$zipper = new \Chumper\Zipper\Zipper;
$zipper->make(storage_path('app/' . $zipPath));
.
.
Storage::makeDirectory($zipPath . DIRECTORY_SEPARATOR . $user->username , 0777);
$zipper->folder($zipPath . DIRECTORY_SEPARATOR . $user->username);
$currentZipPath = $zipPath . DIRECTORY_SEPARATOR . $user->username;
Storage::makeDirectory($currentZipPath . DIRECTORY_SEPARATOR . $myfolder->name , 0777);
$zipper->folder($currentZipPath . DIRECTORY_SEPARATOR . $myfolder->name);
.
.
$this->addDataToZip($contents, $currentZipPath . DIRECTORY_SEPARATOR . $myfolder->name, $zipper, $user);
Log::info('Zip status : ' . $zipper->getStatus()); //Gives "No error"
$zipper->close();
}
public function addDataToZip($contents, $path, &$zipper, $user)
{
foreach ($contents as $content) {
$filebasepath = storage_path('app/' . $path);
Storage::copy(
$this->model->getActiveStorageBasePath($user)
. DIRECTORY_SEPARATOR . $content->unique_name,
$path . DIRECTORY_SEPARATOR . $content->unique_name
);
$zipper->add(storage_path() . DIRECTORY_SEPARATOR . 'app'
. DIRECTORY_SEPARATOR . $path
. DIRECTORY_SEPARATOR . $content->unique_name);
}
}
Now when I view the folder locally, the entie admin.zip folder is created with complete heirarchy and content.
But on $zipper->close, the zip file is not created and the admin.zip folder remains there.
Also there is no error in the API call or the logs.
Please guide on where I might be making an error
See with example Chumper/Zipper. This is the simple and straightforward way to use.
OR you can place this line "status ::". $zipper->status . "\n" after $zipper->add() to check status.
I build a function that if you give it an array of paths it will look for a file in each path and if you give it an array of one path it will look in that path only for the file.
My questions, is when you give it an array of paths such as:
$array = array(
'template_view_paths' => array(
'path_name' => some/path/
'path_name_two' => some/path/two/
)
)
It seems to find the file in some/path/ but keeps going on to some/path/two/ and freaks out because the file doesn't exist. What would I have to change so that if it find the file in any path that it stops looking in other paths?
The code - NOT re-factored
public function render_view($template_name){
if(!isset($this->_options['template_view_path'])){
throw new AisisCore_Template_TemplateException('Not view path was set.');
}
if(is_array($this->_options['template_view_path'])){
foreach($this->_options['template_view_path'] as $template=>$path){
require_once($path . $template_name . '.phtml');
}
}else{
if(!file_exists($this->_options['template_view_path'] . $template_name . '.phtml')){
throw new AisisCore_Template_TemplateException('Could not find: ' . $template_name . '.phtml at ' . $path);
}
require_once ($this->_options['template_view_path'] . $template_name . '.phtml');
}
}
Note: the part to focus on is the if(is_array()){} loop.
Shouldn't:
if(is_array($this->_options['template_view_path'])){
foreach($templates as $template=>$path){
require_once($path . $template_name . '.phtml');
}
}else{
be:
if(is_array($this->_options['template_view_path'])){
foreach($this->_options['template_view_path'] as $template=>$path){
if (file_exists($filename = $path . $template_name . '.phtml')) {
require_once($filename);
return;
}
}
throw new AisisCore_Template_TemplateException('Could not find: ' . $template_name . '.phtml in any paths');
}else{
You're never actually looping $this->_options['template_view_path'].
im trying to create a Zend Framework custom Provider and here:
if($module == 'default'){
$modelsPath = dirname(__FILE__) . '/../application/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else
if(!$module == ''){
$modelsPath = dirname(__FILE__) . '/../application/' . $module . '/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else {
die('Please enter a module!');
}
when i im trying to create a path to the file and when its default module everything is ok, but when for ex. module is other word no matter what the realpath returns false?! where is the error?
try using the APPLICATION_PATH constant.
if($module == 'default'){
$modelsPath = APPLICATION_PATH . '/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else
if(!$module == ''){
$modelsPath = APPLICATION_PATH . '/' . $module . '/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else {
die('Please enter a module!');
}
Taken out of the php manual:
realpath() returns FALSE on failure, e.g. if the file does not exist.
Note:
The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE.
So the problem most probably is with folder permission or the path simply is wrong!
Double check that the paths exist by simple echoing $filePath. If this checks out to be okay then ensure that the 'user' (apache, www-data, for example) have enough privileges. If you are not sure who your server is operating as you can simply debug by echoing whoami
I'm just trying to build my first app on PHP Fog but there's a piece of code that doesn't run properly - works fine on localhost and other regular hosts though.
I use a modified version of TinyMVC, this is the code responsible for setting up autoloading:
/* Set include_path for spl_autoload */
set_include_path(get_include_path()
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'core' . DS
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'libraries' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'controllers' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'models' . DS
);
/* File extensions to include */
spl_autoload_extensions('.php,.inc');
/* Setup __autoload */
$spl_funcs = spl_autoload_functions();
if($spl_funcs === false)
spl_autoload_register();
elseif(!in_array('spl_autoload',$spl_funcs))
spl_autoload_register('spl_autoload');
Basically, it fails at the first class it should load, which is located in "FRAMEWORK_BASEDIR . 'core' . DS". The class filename is "framework_controller.php" and class name is "Framework_Controller" (tried lowercase as well). If I include the class manually it works but fails with autoload.
Here's the error message that I get:
Fatal error: spl_autoload(): Class Framework_Controller could not be loaded in /var/fog/apps/app7396/claudiu.phpfogapp.com/application/controllers/home.php on line 12
Any ideas as to what could the problem be?
I managed to sort it out:
function framework_autoload($className, $extList='.inc,.php') {
$autoload_paths = array (
FRAMEWORK_BASEDIR . 'core' . DS,
FRAMEWORK_BASEDIR . 'libraries' . DS,
FRAMEWORK_APPLICATION . DS . 'controllers' . DS,
FRAMEWORK_APPLICATION . DS . 'models' . DS
);
$ext = explode(',',$extList);
foreach($ext as $x) {
foreach ($autoload_paths as $v) {
$fname = $v . strtolower($className).$x;
if(#file_exists($fname)) {
require_once($fname);
return true;
}
}
}
return false;
}
spl_autoload_register('framework_autoload');
Thanks to another question here on StackOverflow: spl_autoload problem