I am trying to zip multiple folders in codeigniter.
here is my code
public function zip_files()
{
$this->load->library('zip');
$this->zip->compression_level = 0;
$path1 = '/my folder path';
$this->zip->read_dir($path1);
$path2 = '/my another folder path';
$this->zip->read_dir($path2);
$this->zip->download('my_upoads.zip');
}
but the above code zips only the $path2 folder.
how to zip multiple folders? this is my question
Thankyou,
syed
you can use clean data
$this->zip->clear_data();
example :
$name = 'my_bio.txt';
$data = 'I was born in an elevator...';
$this->zip->add_data($name, $data);
$zip_file = $this->zip->get_zip();
$this->zip->clear_data();
$name = 'photo.jpg';
$this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
$this->zip->download('myphotos.zip');
You can use $this->zip->read_dir()
refer:
https://ellislab.com/codeigniter/user-guide/libraries/zip.html
$this->load->library('zip');
$path = '/path/to/your/directory/';
$this->zip->read_dir($path);
$this->zip->download('my_backup.zip');
you have to supply base folder then it will zip all folders, files within it in single zip file.
Related
I'm trying to store image in 3 different folder inside public folder now I'm able to store in two different folder but when I added 3rd folder path but it was not coping in 3rd folder help me to solve this.
my two folder copying Working code
$input['student_photo'] = time().'.'.$request->student_photo->getClientOriginalExtension();
$folder1 = public_path('public/path1/');
$path1 = $folder1 . $input['student_photo']; // path 1
$request->student_photo->move($folder1, $input['student_photo']); // image saved in first folder
$path2 = public_path('public/path2/') . $input['student_photo']; // path 2
\File::copy($path1, $path2);
I tired this code for copy 3rd folder but not working
$input['student_photo'] = time().'.'.$request->student_photo->getClientOriginalExtension();
$folder1 = public_path('public/path1/');
$path1 = $folder1 . $input['student_photo']; // path 1
$request->student_photo->move($folder1, $input['student_photo']); // image saved in first folder
$path2 = public_path('public/path2/') . $input['student_photo']; // path 2
$path3 = public_path('public/path3/') . $input['student_photo']; // path 3
\File::copy($path1, $path2, $path3);
\File::copy($path1, $path2);
\File::copy($path1, $path3);
If you are familiar with Ne-Lexa/php-zip, I want to upload all my extracted files in s3 bucket. I tried this code but instead, it is storing in my public folder instead of s3.
$file = $request->file('filename');
$filename = $file->getClientOriginalName();
$zipFile = new \PhpZip\ZipFile();
$zipFile->openFile($file)
->extractTo(Storage::disk('s3')->makeDirectory($directory));
$zipFile->close();
I'm trying to get all files from a directory and store it inside a variable the process is like When I upload a file,i store it inside a variable ,in this scenario the process will be same but i wont uploading anything , I will be directly sending those files ,
Any help would be much appreciated
This what I tried
$dir = public_path('dbf');
// $new_path = public_path('new_files');
$files1 = scandir($dir);
$count=count($files1);
for($i=1;$i<$count;$i++)
{
$file=$files1[$i];
if($files1[$i]!='.' && $files1[$i]!='..')
{
$data=file_get_contents($dir.'/'.$file);
$name=$data->getClientOriginalName;
echo $name;
}
}
Its showing me error
Trying to get property 'getClientOriginalName' of non-object
Please solve this for me
inside that folder there is a file named cams_transaction.dbf
content of that file
I have a Laravel 5.3 app that has a form which users can upload multiple files using multiple file fields. The form work in that the files can be uploaded and moed to the destinationPath as I expect but I can't seem to change each of the files 'filename' values. It keeps saving the filename value as the php**.tmp.
Here is the foreach in my controller;
$files = $request->files;
foreach($files as $file){
$destinationPath = 'images/forms'; // upload path
$filename = $file->getClientOriginalName(); // get image name
$file->move($destinationPath, $filename); // uploading file to given path
$file->filename = $filename;
}
If I dd($filename) and dd($file->filename) within the foreach I do get the value (original name) I am looking for but if I dd($files) outside that foreach, the filename is set as the temp php value.
What am I missing? Thanks.
EDIT
The file object looks like this;
-test: false
-originalName: "sample_header_1280.png"
-mimeType: "image/png"
-size: 51038
-error: 0
path: "C:\xampp\tmp"
filename: "php7240.tmp"
basename: "php7240.tmp"
pathname: "C:\xampp\tmp\php7240.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\php7240.tmp"
I am trying to save the originalName to the db but it seems to default to saving the filename.
Turns out using a foreach for Input::file is not he approach here. If uploading multiple files from the same field - then you'd use a foreach to loop, move and save.
To upload files from multiple file inputs on the same form all you need to do is treat each input individually - as you might with any other form.
In my example I did this in my controller;
$data['image1'] = Input::file('image1')->getClientOriginalName();
Input::file('image1')->move($destinationPath, $data['image1']);
$data['image2'] = Input::file('image2')->getClientOriginalName();
Input::file('image2')->move($destinationPath, $data['image2']);
Not sure this is the best approach (there's always another way) but it worked for me.
I can change my code to save the uploaded image in the public dir but not when I want to their uploaded image in a folder as their company's name. For example of what works:
/public/company_img/<filename>.jpg
If the user's company name is Foo, I want this when they save save their uploaded image:
/public/company_img/foo/<filename>.jpg
This is in my controller:
$image = Input::file('company_logo');
$filename = $image->getClientOriginalName();
$path = public_path('company_img/' . Auth::user()->company_name . '/' . $filename);
// I am saying to create the dir if it's not there.
File::exists($path) or File::makeDirectory($path); // this seems to be the issue
// saving the file
Image::make($image->getRealPath())->resize('280', '200')->save($path);
Just looking at that you can easily see what it's doing. My logs shows nothing and the browser goes blank after I hit the update button. Any ideas
File::exists($path) or File::makeDirectory($path);
This line does not make sense, as you check if a file exists and if not you want to attempt to create a folder ( in your $path variable you saved a path to a file not to a directory )
I would do something like that:
// directory name relative to public_path()
$dir = public_path("company_img/username"); // set your own directory name there
$filename = "test.jpg"; // get your own filename here
$path = $dir."/".$filename;
// check if $folder is a directory
if( ! \File::isDirectory($dir) ) {
// Params:
// $dir = name of new directory
//
// 493 = $mode of mkdir() function that is used file File::makeDirectory (493 is used by default in \File::makeDirectory
//
// true -> this says, that folders are created recursively here! Example:
// you want to create a directory in company_img/username and the folder company_img does not
// exist. This function will fail without setting the 3rd param to true
// http://php.net/mkdir is used by this function
\File::makeDirectory($dir, 493, true);
}
// now save your image to your $path
But i really can't say your behaviour has something to do with that... Without error messages, we can only guess.