codeigniter directory map helper - folder permissions - php

I am using dircetory_helper() to list all directories and files. However, when I change the permissions of a folder to 0700 (so it cannot be seen or accessed), it is still appearing in the array. Like so;
Array
(
[2001-07-01/] => Array
(
[0] => 1_july_2001.pdf
)
[0] => introduction.html
[2009-05-01/] =>
[2012-07-01/] => Array
(
[0] => 1_july_2012.pdf
[1] => 1_july_2012.xls
)
[2013-01-01/] => Array
(
[0] => 1_january_2013.pdf
[1] => key_points.html
[2] => 1_january_2013.xls
)
)
Look at the 2009-05-01/ key. I do not want this to appear in the array. At the moment it is appearing as an array key but what is the item? Is it NULL?
Is there a fix for this? I am using codeigniter version 2.1.3

At the moment it is appearing as an array key but what is the item? Is it NULL?
It is an empty array. Is is boolean (false). So long as it's not showing the contents, what is the problem? When you are displaying the array, there isn't anything in the array so it (nor it's key) should be displayed.
if( count($array_item) == 0 )
{
// dont show $array_item
}
else
{
// $array_item with stuff in it; display
}
I don't think there is a way to prevent the key from getting in there without changing the permissions on the parent directory which will then obviously prevent the rest of the dir's from being read too. By changing the folder to 0700, it is acting as expected by not rendering the contents of the directory.
However, you can modify the result of directory_map() before sending it back:
$this->load->helper('directory');
$map = directory_map('testdir');
$my_map = array();
foreach($map as $k => $v )
{
// check that it's not an empty array and that it's not a file
// in the root directory
if($v and gettype($v) == 'array')
{
$my_map[$k] = $v;
}
}
print_r($my_map);

Related

Dynamically finding a particular array index

I have a large multi-dimensional array with multiple occurrences of #options indexes. The following is a single array example:
FORM => Array
(
[#attached] => Array
(
[library] => quiz/quiz-form-styling
)
[text_0] => Array
(
[#type] => markup
[#markup] =>
Wherelese did Walter White work besides being a teacher?
)
[radio_1] => Array
(
[#type] => radios
[#options] => Array
(
[0] => An elder Care home
[1] => [A car wash]
[2] => A beauty saloon
[3] => For Skylers old boss
)
[#correct] => testing_correct_for radio
)
[text_2] => Array
(
[#type] => markup
[#markup] =>
)
)
In the example above, the parent array of #options is radio_1. But that is not always the case as the arrays are dynamically generated. There is no way to know in advance what the parent index would be but there is always an #options index.
What I'm trying to figure out is how to find and retrieve the data in all occurrences of #options. How can I do that?
I'd suggest iterating the set of form elements and checking if an inner #options key exists. If so, you can add the options to your array of all options.
$all_options = [];
foreach ($form_elements as $name => $settings) {
if (isset($settings['#options'])) {
$all_options[$name] = $settings['#options'];
}
}
I used the element name as key in the example code, because I thought it seemed like it would be convenient to know where the options came from, but you wouldn't have to do it that way. If you just wanted them all in one big list, you could merge them onto $all_options instead of appending them.
$all_options = array_merge($all_options, $settings['#options']);
This is assuming each of the values under FORM is an array representing one form element. If there is any nesting such that #options could appear at a deeper level, a recursive search could handle that, but if not, I think it's best to keep it simple.
You can try something like recursive function
Here is simple example for above case.
$alloptions = array();
function seach($searcharray){
foreach($searcharray as $key=>$value){
if($key == '#options'){
$alloptions[] = $searcharray[$key];
}else if(is_array($value)){
seach($value);
}
}
}

Identify if array list is a file or folder code igniter FTP class

OK i am using code igniter FTP class to connect to a server and retrieve the list of files and folders and store that list in an array, Now the issue is how can i identify which array item is a folder and which one is a file.
This is the output
Array
(
[list] => Array
(
[0] => ./Directory 1
[1] => ./Directory 2
[2] => ./Directory 3
[3] => ./New Text Document.txt
)
)
try is_dir() and is_file().
Also be careful of symlinks, which are neither:
us1.php.net/manual/en/function.is-link.php
Thank you Nick J for telling me about file path, finally solved it, credits to you. Here is how i did it
$data['list'] = $this->ftp->list_files();
foreach ($data['list'] as $item){
$path_parts= pathinfo($item);
if (isset($path_parts["extension"]))
{
echo 'File: '.$item.'';
}
else {
echo 'directory:'. $item.'';
}

Getting Array Data

The following is a profile array printed using r_print:
Array
(
[0] => Array
(
[Title] => Profile
[Name] => Veritas
[NKey] => Key_1
[SKey] => Key_2
)
[1] => Array
(
[Title] => Access
[Admin] => True
[Banned] => False
[Public] => True
)
)
What I'm trying to do, is simply retrieve elements of that array.
IE,
$profile[] = ...; //GET_ARRAY
$user = $profile[0]['Name'];
$key_1 = $profile[0]['NKey'];
$key_2 = $profile[0]['SKey'];
$admin = $profile[1]['Admin'];
For some reason, the above code doesn't work, though logically it should work without a problem.
What IS returned, is just the character 'A' if I target anything within the array.
You are adding another level to your array by assigning the array to $profile[]. The brackets turn $profile into an array and then adds that array to it causing the extra level.
$profile[] = ...; //GET_ARRAY
should just be
$profile = ...; //GET_ARRAY
Figured out what I was looking for, thought PHP automatically formated arrays (string data) passed from another page. I solved my issue using serialize() & unserialize().

PHP - Comparing two multidimensional arrays

I have two arrays with data in them and I need to compare the two and create one final array.. here is my situation:
// grab a list of the folders
$folders = glob("../*",GLOB_ONLYDIR);
// create empty array's which will contain our data
$projects_data = array();
$folders_array = array();
// list the contents of the config file
$data = json_decode(file_get_contents('.my-config'), true);
// loop through our data file
foreach($data['web_app']['projects'] as $project) :
// update our projects data array
$projects_data[] = $project;
endforeach;
// loop through each folder on our localhost
foreach($folders as $folder) :
// update our folders array
$folders_array[] = array(
'folder' => basename($folder),
'last_modified' => filemtime($folder),
'dir_size' => dirsize($folder)
);
endforeach;
so I have two arrays.. like so:
$projects_data array
Array
(
[0] => Array
(
[folder] => GitHub Clones
[last_modified] => 1379974689
[dir_size] => 6148
)
[1] => Array
(
[folder] => MagentoPlayground
[last_modified] => 1380336582
[dir_size] => 82340978
)
[2] => Array
(
[folder] => Projects
[last_modified] => 1380581312
[dir_size] => 5954
)
)
$folders_array array
Array
(
[0] => Array
(
[folder] => MagentoPlayground
[last_modified] => 1380336582
[dir_size] => 82340978
)
[1] => Array
(
[folder] => Projects
[last_modified] => 1380581312
[dir_size] => 5933
)
[2] => Array
(
[folder] => old
[last_modified] => 1371064970
[dir_size] => 63385844
)
)
I need to compare these two arrays.. If there is one that exists in the top array and does not exist in the second array (Github Clones) then I need to remove it. If there is one that exist in the bottom array that does not exist in the top array (old) then I need to add it. I guess I will need a third array with the new data but I'm not sure how to structure this.
Also, if there are two entries in both arrays (MagentoPlayground) I need the new array to use the data from the bottom array. The bottom array will have the most up to date last_modified stamp and directory size.
Thanks for any help.
I'd compare using the rules you've just mentioned:
Exists in A but not in B -> remove
Exists in B but not in A -> add
...and create a third and final array. Due to the first rule, you may as well loop through array B as comparison which will solve that one.
<?php
// multidimensional array key search (one deep)
function m_array_key_exists($key, $array) {
foreach($array as $subkey => $subvalue) {
if($subkey === $key)
return true;
if(is_array($subvalue)){
if(array_key_exists($key, subvalue))
return true;
}
}
return false;
}
?>
Seems from those two rules alone that you may as well just take your second array, because if it exists in both arrays it can stay, if it doesn't exist in B you are going to remove it, but it's not there anyway, and if it exists in B but not A you add it, but it's already there...
Use m_array_key_exists as above to check one level deeper than array_key_exists() whether an array key exists in arrays like you've shown. If your rules aren't as simple as I've thought they are, it sounds to me like you want to loop through your second array, check for array keys, apply your special rules and add the result to the third array.

PHP - Check that value doesn't appear twice in array

I have a array inside my PHP app that looks like this:
Array
(
[0] => Array
(
[name] => Name1
[language] => 1
)
[1] => Array
(
[name] => Name2
[language] => 1
)
)
How can I check that "language" with value 1 doesnt appear twice, as effectively as possible?
$dupe = 0;
foreach($yourarray as $key => $val) {
if(array_key_exists($seen, $val['language'])) {
// a duplicate exists!
$dupe = 1;
// could do other stuff here too if you want,
// like if you want to know the $key with the dupe
// if all you care about is whether or not any dupes
// exist, you could use a "break;" here to early-exit
// for efficiency. To find all dupes, don't use break.
}
$seen[$val['language']] = 1;
}
// If $dupe still = 0 here, then no duplicates exist.
Tried the PHP function array_unique?
(Read the comments/user contributed notes below, especially the one by regeda at inbox dot ru, who made a recursive function for multi-dimensional arrays)

Categories