Trying to get list of views in folder in Codeigniter - php

I'm using Codeigniter, and all I'm trying to do is to get a list of all the views in a particular folder within the "views" folder.
Here's my code, inside of my controller:
$this->data['sections'] = array_diff( scandir('/application/views/portfolio/embeds/work/'), array('.', '..') );
And here is the error message I'm getting:
Message: scandir(/application/views/portfolio/embeds/work/): failed to open dir: No such file or directory
Filename: controllers/Work.php
Line Number: 54
Am I just forgetting something? Or is there maybe a way to set a relative path from the location of my controller? Because using the path "../views/portfolio/embeds/work/" doesn't seem to work either.

You need to add in the base path of your installation. Currently you are asking scandir to read the directory /application on your web server, when you most likely want /var/www/example.com/application
Try this:
scandir(FCPATH . 'application/views/portfolio/embeds/work')

Try like in a foreach loop and use FCPATH
$files = array_diff(scandir(FCPATH .'application/views/portfolio/embeds/work'), array('.', '..'));
foreach ($files as $file) {
echo $file;
}

Related

failed to open stream: Permission denied open server

My aim is to download multiple files into the folder on my localhost. I am uploading them using the HTML form.
Here is the code (really sorry that I can't give a link to the executable version of the code because it relies on too many other files and database if anyone knows the way then please let me know)
foreach ($_FILES as $value) {
$dir = '/';
$filename = $dir.basename($value['name']);
if (move_uploaded_file($value['tmp_name'],$filename)) {
echo "File was uploaded";
echo '<br>';
}
else {
echo "Upload failed";
echo '<br>';
}
}
So this little piece of code give me an error:
And here are the lines of code:
The problem is that the adress is correct, I tried enterring it into my file directory and it worked fine, I have seen some adviced on other people's related questions that // or \ should be used instead, but my version works just fine! Also I have checked what's inside the $_FILES and here it is if that's required for someone trying to help:
Thank you very much if anyone could help!!
You are trying to move the file to an invalid (or non-existent) path.
For the test you will write
$dir = 'c:/existing_dir/';
$filename = $dir.basename($value['name']);
If you want to move the file to a folder that is relative to the running file try
$dir = '../../directory/';// '../' -> one directory back
$filename = $dir.basename($value['name']);
By starting your file path with $dir = '/'; you are saying store the file on the root folder, I assume of C:
Apache if correctly configures should not allow you access to C:\
So either do
$dir = '../';
$filename = $dir.basename($value['name']);
to make it a relative path or leave the $dir = '/'; out completely

Codeigniter : Delete directory after deleting all files

I want to delete a folder after deleting all the files inside it, in my codeigniter project. Lets say my folder name is upload, which is located near the application folder in my ci project.
The upload folder contains Peniyal as a sub-folder and it contains 4 images inside it. I need that 4 images to be deleted, next the sub-folder has to be deleted.upload folder should not be deleted. I am hanging my mind to do it.
So far I have tried the following:-
$files = glob('./upload/Peniyal');//to get all file names
//am not sure whether the path is correctly given..
foreach($files as $file){ // iterate files one by one
if(is_file($file))
unlink($file); // delete file
}
$path = './upload/Peniyal';
rmdir($path);
Any help will be appreciated. Thx!
The glob() function matches a pattern, but you are not providing one. So if you use the pattern *.* the glob will find all files in that folder.
// match any file
$files = glob('./upload/Peniyal/*.*');
foreach($files as $file){
if(is_file($file))
unlink($file);
}
$path = './upload/Peniyal';
rmdir($path);
With CodeIgnitor 4:
delete_files('./path/to/directory/', true);
rmdir('./path/to/directory/');

How to select a folder inside another without having to know the name

How would I select the folder inside of another without having to know the name with PHP? Their is only one folder inside of the other. I know you can use ../../ to get folders above a directory and would assume that their is a method to do the same with folders down a directory.
If your system is linux based, depending on your specific usecase the following might work:
/folderwithonedir/*/
You can use scandir() or glob() to get the contents of the directory and then your folder will probably the first or second item in the result, you can check that with is_dir() in a loop.
$data = array();
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
if (is_dir($file) === true)
{
$data[] = strval($file);
}
}
$data has your directories

Recursive File listing stops after 2 files

I have the following code to list all files (in the direct folder and sub folders).
$imagesDir = '../Stock_Images';
$di = new RecursiveDirectoryIterator($imagesDir);
foreach (new RecursiveIteratorIterator($di) as $filename => $file)
{
echo $file->getPathname() . '<br/>';
}
The problem is that I have 2 files named ".apdisk" and ".DS_Store".
The script list both of the files and then stops like there's a bug or something.
I deleted one of the files and then it worked as it should....
Any idea why it happens and how to fix that?
Thanks
EDIT:
I also had FOLDER named ".TemporaryItems" < dont ask me why.
It was the problem >
Caught exception: RecursiveDirectoryIterator::__construct(../Stock_Images/.TemporaryItems/folders.501): failed to open dir: Permission denied
just deleted that folder and it fixed the problem.

PHP opendir issue

Why do I get this error even though the directory exists? it works fine if I target the parent directory, I tried using %20 instead of space too, and tried removing the last / but nothing works!
Warning: opendir(/home/xxxx/user_files/users/xxxx/test directory/) [function.opendir]: failed to open dir: No such file or directory in /home/xxxx/public_html/beta/stream._pages/file._list._i.php on line 54
(Note: xxxx is just me censoring user names)
Make a file called test.php and put it in your test directory. In that file, put this code:
<? echo dirname(__FILE__);?>
Then, visit test directory/test.php in your web browser, copy and paste the path as given in test.php and try using that exact path in opendir.
Another issue might be that the permissions of your directory aren't right, try chmodding to 777
For anyone trying to find the folder outside of the public_html folder.
This code is provided by php.net for the opendir() function:
if ( $handle = opendir('../../../../') )
{
echo "Directory handle: $handle\n";
echo "Entries:\n";
/* This is the correct way to loop over the directory. */
while ( false !== ( $entry = readdir( $handle ) ) )
{
echo "$entry\n";
}
/* This is the WRONG way to loop over the directory. */
while ($entry = readdir($handle))
{
echo "$entry\n";
}
closedir( $handle );
}
Solution
The $handle I started checking out how far I could go back with '../' adding as much ../ as possible until you find yourself in the folder you need. From there you take I guess.
For me '../../../../' was enough to get there, it's different on every server.

Categories