download existing zip folder from laravel storage - laravel

I have a zip folder under storage and I want to download this folder whenever I click a button. But my code does not work. What is the problem?
here's my livewire component
<div class="my-auto">
<a type=button wire:click="downloadKit" style="border-radius:4px" class="">{{ __('Download') }}</a>
</div>
public function downloadKit(){
$file = Storage::disk('public')->get('kit.zip');
return (new Response($file, 200));
}

You can return a file response like this
return Storage::download("/path/to/kit.zip", "kit.zip");
The path will be relative to /storage/app

Related

Images not showing up on a mac

I have setup up a simple file upload system using laravel.
To store the image, I set up my controller as
public function store(Request $request)
{
$menu = new Menu;
$menu->title = $request->get('menuTitle');
$menu->user_id = Auth::id();
if($request->hasFile('menuImage')){
$filenameWithExt = $request->file('menuImage')->getClientOriginalName();
$fileName = pathinfo($filenameWithExt,PATHINFO_FILENAME);
$ext = $request->file('menuImage')->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$ext;
$path = $request->file('menuImage')->storeAs('public/menu',$fileNameToStore);
}
else{
$fileNameToStore = 'blank.jpg';
}
$menu->image_name = $fileNameToStore;
$menu->save();
return redirect(route('menu.create'))->with('success',$menu->title." Added successfully");
}
In the view I set it up as
<a href="#">
<img class="img-fluid rounded mb-3 mb-md-0" src={{asset("/storage/menu/".$menu->image_name)}} alt="">
</a>
In the browser the link is
<img class="img-fluid rounded mb-3 mb-md-0" src="http://127.0.0.1:8000/storage/menu/Screenshot_20190517_023111_1558916284.png" alt="">
While developing, I was working on ubuntu and everything works fine. However, I transferred my files to my mac and using xampp I was able to get everything working. However the images do not load anymore. I get a 404 error when I check the console. However, the images are able to upload to the right folder.
You should run php artisan storage:link so that the storage/app/public will be linked to public/storage directory and your link will work again

how to access files stored in folder with its name fetched from database.(laravel)

i have upload section that stores files name in user's table, and files in Folder named as File.
i want to access that file from name stored in database.
i.e if it is a zip file it should get downloaded, if doc, pdf etc it should get open online.
my code in controller that fetches files:
$users = DB::table('users')->select('attachments')->where('id',$id)->first();
$attach = explode(",", $users->attachments);
return View::make('admin.RegisterStaff.show')
->with('attach', $attach);
my code in blade file to display files:
<div class="col-md-4">
#foreach($attach as $data)
{{ $data }}<br>
#endforeach
</div>
what is the best way to access files..??
You can try
#if($data->getClientOriginalExtension() == 'docx')
//do anything you want
#endif

how to convert folder into Zip file in codeigniter where folder contains some image file

i have some doubts in zipping the folder in codeigniter.Actually i have some image files in the folder and i was trying to zip this folder in codegiter. I have view file where i have written "Download all file" button. On clicking this button i have to take the folder and zip it.
My view file:
I have written the button "Download All Files" inside the anchor tag.
<tr>
<td align="right">
<?php if(isset($AppData) && !empty($AppData)) {
$applicantNo = $AppData[0]['appid']; ?>
<a href='<?php echo base_url("admin/downloadAllFileZip/$applicantNo");?>'><input type="submit" name="download" value="Download All Files" /></a>
<?php } ?>
</td>
</tr>
Controller code:
Inside admin.php controller file i have the below method
public function downloadAllFileZip($appId)
{
$path = file_get_contents(base_url().'files/'.$appId);
$applicant_id = $appId;
$this->zip->add_data($applicant_id,$path);
$this->zip->archive('/files/'.$applicant_id);
$this->zip->download($applicant_id);
}
But when I am clicking the " Download all file " button the popup window for zipping is comming and after downloading its showing 1kb file but there is no image on that file. I can able to zip single image file as a zip file but not able to zip the entire folder. Please help me how to do this.
add your all file like this. For Ex:
foreach ($query->result() as $row)
{
$this->zip->read_file($row->filename);
}
$this->zip->download('files_backup.zip');
To archive all files within a folder use $this->zip->read_dir(); method. Example,
$path = '/path/to/folder';
$this->zip->read_dir($path);
$this->zip->archive('/path/to/folder/foldername.zip');
For more info. please have a look: https://www.codeigniter.com/user_guide/libraries/zip.html
Zip library permits you to create Zip archives
$this->load->library('zip');
Give path of your folder that you want to add in archive
$file_name='backup['.date('d-m-Y-H:i:s').'].zip';
$this->zip->read_dir($_SERVER['DOCUMENT_ROOT']."/uploads/images",FALSE);
$this->zip->download($file_name);
I think give absolute path is better way for archived folder on specific folder
$this->load->library('zip');
$path = '/path/to/your_directory/';
$this->zip->read_dir($path);
$this->zip->download('my_backup.zip');

How to remove link when used to create PDF in laravel dompdf?

I just make a simple html page and write content to it. I created a link when I click on that link the content of the page will converted into PDF file, I have implemented this using laravel dompdf -> https://github.com/barryvdh/laravel-dompdf . Now, My problem is that all things doing good, but the PDF that is generated after click will shows that link in the PDF file. How to remove that button from PDF file.
My code :
My controller : ItemController.php
class ItemController extends Controller
{
public function pdfview(Request $request)
{
if($request->has('download')){
$pdf = \PDF::loadView('pdfview');
return $pdf->download('pdfview.pdf');
//return $pdf->stream();
}
return view('pdfview');
}
}
My route: web.php
Route::get('/pdfview',array('as'=>'pdfview','uses'=>'ItemController#pdfview'));
My view : pdfview.blade.php
<body>
<div class="container">
<div class="rows"><br/>
Click to PDF
<h2 align="Center"> What is PDF ? </h2><br/><br/>
<h4> Portable Document Format (PDF) is a file format used to present and exchange documents reliably, independent of software, hardware, or operating system.</h4>
</div>
</div>
</body>
Help me, If you understand my problem. Thanks
Pass a variable to the view, i.e:
$pdf = \PDF::loadView('pdfview', array('pdf' => true));
...
return view('pdfview', array('pdf' => false));
and check the variable in the template:
#if(! $pdf )
Click to PDF
#endif

Magento configuration popup window - where should html file go in module hierarchy?

In my module config, I have a button. When that button is clicked I pop up a window that gathers some more input. That file is just an html file, but where should it live in the directory structure of the module?
To give a bit more info - just to see something working, I defined my field as follows:
<button_url_test_window_open><![CDATA[/px.html]]></button_url_test_window_open>
<frontend_model>mymodule/adminhtml_system_config_testWindowOpenDialog</frontend_model>
and I put the px.html file in my htdocs/magento folder. When I click the button it results in /px.html being opened, but that doesn't seem right. I'm not sure how to word the question, but I feel I should be doing something more like 'open the file called px.html for mymodule' and magento would then look in the right place. Sorry about the terminology, I'm still getting to grips with Magento/PHP/Apache.
Just to complete the picture of what I currently have, the frontend_model block is:
protected function _prepareLayout()
{
parent::_prepareLayout();
if (!$this->getTemplate()) {
$this->setTemplate('mypackage/system/config/test_window_open_dialog.phtml');
}
return $this;
}
public function render(Varien_Data_Form_Element_Abstract $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$originalData = $element->getOriginalData();
$this->addData(array(
'button_label_test_window_open' => Mage::helper('mymodule')->__($originalData['button_label_test_window_open']),
'button_url_test_window_open' => $originalData['button_url_test_window_open'],
'html_id' => $element->getHtmlId(),
));
return $this->_toHtml();
}
and the test_window_open_dialog.phtml file contains:
<table>
<tr>
<td>
<button style="" onclick="javascript:window.open('<?php echo $this->getButtonUrlTestWindowOpen()?>', 'testing','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, ,left=100, top=100, width=600, height=470'); return false;" class="scalable" type="button" id="<?php echo $this->getHtmlId() ?>">
<span><?php echo $this->escapeHtml($this->getButtonLabelTestWindowOpen()); ?></span>
</button>
</td>
</tr>
</table>
phtml files don't go anywhere within your module directory. Module directories house your Blocks, Helpers, Models, Controllers, Sql installation/upgrade files, and your configuration xml files. Template files go in either app/design/adminhtml (if it is an admin template), or in app/design/frontend if it will be used on the frontend of the site.

Resources