Recursive File listing stops after 2 files - php

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.

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

DS_Store file is created on ZipArchive::extractTo

I'm creating a PHP script, which supposed to extract a zip archive stored on the php file directory to a folder.
Everything works well, but when I check te result, I find 2 folders under the directory: a folder with the name of the zip archive, and another folder named __MACOSX. I don't know how this folder came there, especially as I'm using Windows 7. Second, in each folder there is a file called .DS_Store.
Now, I don't know how these things got there. This is my code:
$zip = new ZipArchive;
if ($zip->open('File.zip')) {
$path = getcwd() . "/details/" . trim($id) . "/";
$path = str_replace("\\","/",$path);
echo $path;
echo $zip->extractTo($path);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This is the only code that extracts the zip file, or touching it, and as you can see, there is nothing like __MACOSX or .DS_Store.
Can you please help me?
File.zip originated on a OSX system. __MACOSX and .DS_Store have 0 usage or bearing on any other OS. Delete / Ignore them and keep trucking.
As an aside, you may want to add the stated file system objects to your project .gitignore.
https://superuser.com/questions/104500/what-is-macosx-folder
https://en.wikipedia.org/wiki/.DS_Store

Trying to get list of views in folder in Codeigniter

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;
}

How to correctly select a directory and delete it with its all sub folders and files in PHP

Please answer this question and help me!!
I have found a dozen of code snippets about deleting a folder with its all files and sub folders in PHP. But I am facing difficulties to apply them.
This is an example code :
function rrmdir($path) {
// Open the source directory to read in files
$i = new DirectoryIterator($path);
foreach($i as $f) {
if($f->isFile()) {
unlink($f->getRealPath());
} else if(!$f->isDot() && $f->isDir()) {
rrmdir($f->getRealPath());
}
}
rmdir($path);
}
I can not understand how to write the source directory. I mean, how I set the $path variable? All the files of my website is inside 'public_html' folder. Will I include 'public_html' also?
When I want to delete a folder inside 'usercontent' folder, I am writing like this :
$path = "/usercontent/somefolder"
Please answer with an example directory path.
Update: What should be the permission of the folder which will be deleted.

Uploading files in PHP - Unzipping and Moving

I have a very basic, very straightforward function that takes a file (after checking it to make sure its a zip among other things) and uploads it, unpacks it and such:
public function theme(array $file){
global $wp_filesystem;
if(is_dir($wp_filesystem->wp_content_dir() . "/themes/Aisis-Framework/custom/theme/")){
$target_path = $wp_filesystem->wp_content_dir() . "/themes/Aisis-Framework/custom/theme/";
if(move_uploaded_file($file['tmp_name'], $target_path . '/' . $file['name'])) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo($target_path); // change this to the correct site path
$zip->close();
//unlink($target_path);
}
$this->success('We have uplaoded your new theme! Activate it bellow!');
} else {
$this->error('Oops!', 'Either your zip is corrupted, could not be unpacked or failed to be uploaded.
Please try again.');
}
}else{
$this->error('Missing Directory', 'The Directory theme under custom in Aisis Theme does not exist.');
}
if(count(self::$_errors) > 0){
update_option('show_errors', 'true');
}
if(count(self::$_messages) > 0){
update_option('show_success', 'true');
}
}
Extremely basic, yes I have used my target path as both the path to upload too and unpack (should I use a different path, by default it seems to use /tmp/tmp_name)
Note: $file is the array of $_FILES['some_file'];
My question is I get:
Warning: move_uploaded_file(/var/www/wordpress/wp-content//themes/Aisis-Framework/custom/theme//newtheme.zip): failed to open stream: Permission denied in /var/www/wordpress/wp-content/themes/Aisis-Framework/CoreTheme/FileHandling/Upload/Upload.php on line 82
Warning: move_uploaded_file(): Unable to move '/tmp/phpfwechz' to '/var/www/wordpress/wp-content//themes/Aisis-Framework/custom/theme//newtheme.zip' in /var/www/wordpress/wp-content/themes/Aisis-Framework/CoreTheme/FileHandling/Upload/Upload.php on line 82
Which basically means that "oh the folder your trying to move from is owned by root, no you cannot do that." the folder I am moving too is owned by apache, www-data. - I have full read/write/execute (it's localhost).
So question time:
Should my upload to and move to folder be different?
How, in a live environment, because this is a WordPress theme, will users who have the ability to upload files be able to get around this "you dont have permission"?
You should try to do it the wordpress way. Uploading all user content to wp-content/uploads, and doing it with the native functions.
As you mention, uploading to a custom directory, may be an issue to non tech savvy users. /uploads already have the special permissions.
Check out wp_handle_upload. You just have to limit the mime type of the file.
The path you are trying to upload is some where wrong here
wp-content//themes
try removing one slash

Categories