How to download all files from directory using codeigniter? - php

I am using codeigniter framework for a website. I need to download all the files from a particular directory. I can do this task using PHP alone but not working in codeigniter may be due to some path issue. I searched on many sites but not able to get correct solution. Also, followed official documentation here : codeigniter zip documentation , but without any success. I need working code , which can be tested directly.
code which I have now is :
$path = '/path/to/your/directory/';
$this->zip->read_dir($path);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');

After a lot of tries, finally i found the exact problem. Actually, the CodeIgniter code works perfectly. The problem is setting the path. Here I am zipping a folder named noimg inside assets folder inside CodeIgniter project folder.
function zip()
{
$this->load->library('zip');
$path = FCPATH.'/assets/noimg';
$this->zip->read_dir($path,FALSE);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
}
Don't forget to put 'FCPATH.' before /assets very important. Then only the path can be identified. If you have any problem please comment below. The above code work perfectly for me

Related

Laravel Uploaded file path return error

i been following the upload method using this tutorial and i got some errors that i cant brain anymore.
Here are the example, i upload file name "fileNumber1.pdf", the laravel system will save the file using Storage facade.
$filePath[] = $fileData->storeAs('uploadedFile', $fileName);
So the original path will be "storage/app/uploadedFile/fileNumber1.pdf".
To view the file, the tutorial suggest that to use
echo asset('storage/file.txt');
So from my understanding the echo should be like
echo asset('storage/uploadedFile/fileNumber1.pdf');
Since i use local development the return is
http://localhost:8000/storage/uploadedFile/fileNumber1.pdf
Then i go to the link given, it return
Sorry, the page you are looking for could not be found.
So how is the proper way to receive or view the file?
This url http://localhost:8000/ points to pubilc folder
So store your file inside uploadedFile folder inside public folder then use like
http://localhost:8000/uploadedFile/fileNumber1.pdf
otherwise use storage_path('uploadedFile/fileNumber1.pdf'); instead asset('storage/uploadedFile/fileNumber1.pdf');
If you would have followed the tutorial link then you should have run the below command which creates a symlink, If you haven't, do run that artisan command in your application folder.
php artisan storage:link
So now the below commad will give you desired result.
{{ asset('storage/uploadedFile/fileNumber1.pdf') }}
or
{{ storage_path('uploadedFile/fileNumber1.pdf') }}
You don't have to point the complete path of your file because asset() will defaultly point you to public folder and then you have to add the required path of your file location.
sorry for all the rude if you receive.
The mistake is mine.
The solution was simple.
Original is
$filePath[] = $fileData->storeAs('uploadedFile', $fileName);
The solution is
$filePath[] = $fileData->storeAs('public/uploadedFile', $fileName);
Just add "public" before the file path before uploading.
Thanks all for the effort.

Laravel - Using allfiles gives back empty array

I've been using laravel for nearly all my projects and I've came across a problem when trying to list all files in a directory.
Here is my code below:
$directory = ("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);
So as you can see First line is the directory which is in Upload followed by the username of the account.
The second line is meant to scan this directory and list only files. Sadly it always gives me back an empty array. Using Scandir() here works... But it gives me folders which I'm not wanting.
So... Any tips? I checked the API and I really can't see what is wrong here :(
The reason you're getting no results when using the Storage::allFiles('directory') method is this method only looks inside of your application's storage folder, usually storage/app. Hence why modifying the storage path in configuration works as mentioned by #yassine (you shouldn't do this).
If you wish to use files outside of the storage folder, simply use the File facade instead. e.g. File::allFiles('directory')
Me too i had the same issue for local storage. I fixed it by :
project/config/filesystems.php
line 44
changed root folder to from storage_path('app') to changed to
base_path()
then I provided a related path to the root folder in Storage::files() or Storage::allfiles().
Like Storage::allfiles("app") if I want to fetch files withing the sub folder "app" relative to the base path given in the config file.
Good luck
$direcotry has to be an absolute path from your laravel root folder.
When your uploaded folder is in the root folder try this
$directory = base_path("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);

ci zip encoding class not working properly

I want to crate a zip file and download it.
So my code is as follow;
Since i am using ci zip encoding class
my folder structure is like
c:/xampp/htdocs/gallery/print/oid_1/1.jpg,2.jpg,3.jpg
My code
$path = getcwd().'/print/oid_1/';
$this->zip->read_dir($path,FALSE);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('oid_1.zip');
Here is my problem,
l C:\Users\instinctclassic\Downloads\Compressed\oid_1.zip: The archive is either in unknown format or damaged so what does this means -does it means it was mistaken while making zip file or it was mistaken while downloading.For downloading mistake i have downloaded the zip file many time enough to be sure.
2 I repaired the downloaded zip file(oid_1) and extract it but the extracted folder structure is not ignored before the oid_1 folder as said in ci zip encoding class tutorial
$this->zip->read_dir($path, FALSE);
//This will create a ZIP with the folder "directory" inside, then all sub-folders stored correctly inside that, but will not include the folders /path/to/your.
3 Assuming my folder oid_1 already exists somewhere on server.
I know this question is previously asked but mine exist all the problem from making zip file to extracting.So i am sorry for this.
Thanks
Try directory path like this:
$path = getcwd().'\print\oid_1\';
this way path will be proper, because getcwd() will return you c:\xampp\htdocs\gallery

PHP, MoDX: Upload file from custom PHP Snippet

I have a snippet that I want to use to upload a file.
The script seems to be running fine until it gets to the point where PHP transfers the file from temp docs to my own folder.
My folder is called 'uploads' and is the root.
On ModX the PHP files directory is
/core/cache/includes/elements/modsnippet
I cannot seem to figure out a way to direct the script to send the file back from the modsnippet directory to the public_html/uploads directory.
So far I have tried:
move_uploaded_file($_FILES['instlogo']['tmp_name'], dirname(__FILE__) . "/../../../../uploads".$new_file_name)
;
as well as absolute paths, eg:
http://mysite.com/uploads
To no avail.
Would anyone know a way of doing this correctly? Thanks!
(ps: permissions on that folder are 777)
can you try something like:
$my_uploads = $modx->getOption('base_path').'uploads/';
move_uploaded_file($_FILES['instlogo']['tmp_name'], $my_uploads.$new_file_name)
the base_path option will give you the full file system path [from the server root]
if not & you are getting a filesystem error, the server logs [aapche] should be telling you why. Do you have access to them?

What filepath to use with ffmpeg_php?

I'm using ffmpeg-php to extract thumbnails from user uploaded videos for a site I'm building. Previously the videos were stored in a subdirectory of the directory containing the relevant php files and it worked fine. However I have now altered my directory structure and can't work out what filepath to supply ffmpeg_movie() with. The relevant files are:
/app/classes/class_lib.php
location of the class that calls ffmpeg_movie() and extracts/saves the thumbnail.
/app/upload.php
the php file that requires the above class, instantiates it and calls the relevant method.
And the videos are stored in:
/videos/encodes/
All those paths are relative to the sites public root (public_html). I have trieda number of different paths but keep getting a "cannot open movie file [Attempted video path]". I've tried paths relative to the site root /video/encodes/movie.mp4, relative to the executed php file ../video/encodes/movie.mp4, relative to the php class file ../../video/encodes/movie.mp4 and even the server root /srv/www/sitename.com/public_html/video/encodes/movie.mp4. No luck with any of them.
Any other ideas?
Server is Apache running on Ubuntu and directory permissions haven't changed since it was previously working (the encodes folder is globally readable)
I've ended up using the full url http://somesite.com/video/encodes/movie.mp4. It actually works, but it feels like the wrong way to do it. Unfortunately nothing else seems to work.
Still, if you're having the same problem then this is at least a working solution.

Categories