Laravel - allow to download files from a folder in blade view - arrays

I'm stuck somewhere in the middle and I can't go on. Have folder, in folder files. In blade.view wants them download via link.
$fileNameGlob = glob($directory."*.*", GLOB_BRACE);
dd($fileNameGlob) return array
array:3 [▼
0 => "/var/www/html/domain/storage/app/apiFiles/611106/extract/1.png"
1 => "/var/www/html/domain/storage/app/apiFiles/611106/extract/2.png"
2 => "/var/www/html/domain/storage/app/apiFiles/611106/extract/3.jpg"
]
when i try return response()->download($fileNameGlob) have problem with array. If i have file path how continue to download file ?

Do you want to provide one link to download all files? Then you should zip the files beforehand and then deliver the zipfile path.
You could also use:
return response->download('apiFiles/611106/extract/1.png');
return response()->download('apiFiles/611106/extract/1.png', $name, $headers);
But this would not work for the array, but for the single elements, so you should provide something like $fileNameGlob[i].

Related

CakePHP 3 - Downloading regardless of the host and project name

On any particular View, if I include this link:
<?php echo $this->Html->link('Download', ['controller' => 'Uploads', 'action' => 'download',$upload->id]) ?>
A user can download a file based on the $upload->id provided.
I'm using this as my download function in my Uploads controller:
$upload = $this->Uploads->find('all')->first();
$filePath = '\xampp\htdocs\project\webroot\uploads'; //file path to where the uploaded file is
$this->response->file($filePath . DS . $upload->name,
array('download' => true, 'name' => $upload->name)); //finds the corresponding name attribute in the Uploads table of the database, finds a matching name in the uploads directory and then downloads that file.
$this->set(compact('upload'));
However, I was wondering if there was a way to be able to have the file path be more flexible. In my config.php, I have set two variables called $host and $basepath to refer to the web host (localhost) and the project name (project) respectively. I pass these variables to the AppController, and they can be accessed by any View.
But I can't seem to use these variables in the $filePath variable of the download function, so if I or anyone else were to change hosts or the project name, instead of being able to simply change config.php, I or anyone else would have to go and find this and change it as well (and if I had multiple download functions in multiple controllers, they'd have to go and change each and every one of them).
Take a look at the CakePHP global constants. You can easily access the path to your webroot by using WWW_ROOT.
https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html

audio upload in cakephp2.4

I want to upload audio files (type mp3,acc,wav) I am using cakephp 2.4.1 stable and php5 stable. I have tried mime_content_type,finfo_file to check if uploaded file is audio file with either mp3,acc or wav type. But I get this
`error mime_content_type(059.piya basanti re... [piya basanti][2000].mp3): failed to open stream: No such file or directory [APP/Controller/AdminController.php, line 86]`
My app dir and webroot dir are permited with 0777.
Here is my view.ctp code :
<?= $this->Form->create('Homepage',array('type'=>'file'));
echo $this->Form->input('audio_1',array('type'=>'file'));
echo $this->Form->submit('Submit');
echo $this->Form->end(); ?>
my Controller code
public function saveaudio() {
if($this->request->is('post')) {
$this->loadModel('Homepage');
//this is line 86// $file = mime_content_type($this->request->data['Homepage']['audio_1']['name']);
pr($file);exit;
}
and here is my data
Array
(
[Homepage] => Array
(
[audio_1] => Array
(
[name] => 059.piya basanti re... [piya basanti][2000].mp3
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
)
My question is :
How I can check type of file? First I want to check its mime type then I want to save it
Why I am not getting tmp_name for uploaded file?
Why I am getting error for mime_content_type?
Can anyone explain me? It would be helpfull to me
Look at the data, the upload was discarded as there's an error. 1 equals UPLOAD_ERR_INI_SIZE, and the cause for this is:
The uploaded file exceeds the upload_max_filesize directive in php.ini.
See http://php.net/manual/features.file-upload.errors.php
So you have to increase that value, and most probably also post_max_size, see http://php.net/manual/features.file-upload.common-pitfalls.php for more information.
And once the uploading works, you'll have to use the correct keys, it's audio_1, not audio_, also you'll then have to use tmp_name, ie this:
$this->request->data['Homepage']['audio_']['name']
should be this
$this->request->data['Homepage']['audio_1']['tmp_name']
On a side note, mime_content_type is deprecated in favour of the Fileinfo extension.

how do i include a tpl file in my module drupal 7

I'm building a module using hook_preprocess_node()
I've made a new view mode for the node entity called ´vacancy_teaser´ using the hook_entity_info_alter()
this shows up in my node display settings and view
so I want to use the template included in my module when this view mode is used.
my code:
/**
* Implements hook_preprocess_node().
*/
function vacancies_preprocess_node(&$vars) {
if($vars['view_mode'] == 'vacancy_teaser') {
$vars['theme_hook_suggestions'][] = 'node_vacancy_teaser';
}
}
my template file is called: ´node-vacancy-teaser.tpl.php´ but is not used in the output of my view
$vars['view_mode'] == 'vacancy_teaser' in the view. ( tested )
but where does $vars['theme_hook_suggestions'][] = 'node_vacancy_teaser'; looks for the template file? somehow it's not included / used.
apparently in drupal 7 useing dubble underscores is required for some reason.
node_vacatures_vacancy_teaser.tpl.php placed in the active template folder seems to do the trick... although I don't think this is a neat solution since the tpl.php file is separated from the module.
Be sure to specify the template file in the hook_theme implementation. The examples project is great for finding out the details of how to do things like this. Specifically, check out the theming_example_theme() function in the theming_example module…
function theming_example_theme() {
return array(
// …
'theming_example_text_form' => array(
'render element' => 'form',
// In this one the rendering will be done by a tpl.php file instead of
// being rendered by a function, so we specify a template.
'template' => 'theming-example-text-form',
),
);
}
Instead of appending to the end of the $vars['theme_hook_suggestions'] array, try:
array_unshift($vars['theme_hook_suggestions'], 'node_vacancy_teaser');
This will pass your suggestion to the front of the array, and it will be found first. Most likely since you are appending it to the end of the array, Drupal is finding an existing theme suggestion first and using it instead (such as node.tpl.php).

CakePHP: Updating multiple records with hasMany

I'm working on a website that has a file management portion where users can create folders and upload files. The folders CAN have subfolders. The folders are not actually created on the file system; they are just in the database. The files are created on the file system and information about the files are in the database.
I'm trying to make it so that if a user deletes a folder, it marks that folder as well as its subfolders and files as deleted. So let's say a user deleted a folder called "Main" that had this structure:
Main
Main\Subfolder\file.txt
Main\Subfolder 2 <-- empty folder
Main\Subfolder 3\image.jpg
I am able to mark all of the folders' deleted field with a "Y" like this:
foreach ($folders_to_delete as $folder_to_delete) {
$updateAll_conditions['OR'][] = array('id' => $folder_to_delete);
}
$this->UserFolder->updateAll(array('UserFolder.deleted' => "'Y'"), $updateAll_conditions)
But I want to mark all of the folders' deleted field with a "Y" AND all of the files that belong to those folders... with one query. Is that possible?
This problem becomes even more complicated when you consider that a folder might contain another folder!
You ultimately will want a script that can handle file structures of arbitrary complexity.
I suggest running a recursive function over the file structure in question and add the records that need updating to a massive $this->data array.
Then, do a saveAll (or updateAll) on that array.
Here's what a $this-data array might look like for a saveAll with multiple models affected:
$this->data = array(
'Folder' => array(
0 => array('deleted' => 'Y'),
1 => array('deleted' => 'Y'),
),
'File' => array(
0 => array('deleted' => 'Y'),
1 => array('deleted' => 'Y'),
),
)
I'll keep this answer to the model related stuff, if you want help w/ the recursive function, let's do that that in a separate question.

how to edit a cakephp record without deleting the path to an uploaded file

I want to edit a record in CakePhp without deleting the saved path of an uploaded jpg file. Right now when I edit the record it deletes the saved file path.
The approach I think would work is to do a logic check in the form to check if the field is empty or not. Display the saved file path if it's not empty or show an empty path if it is. Would this be the best way to update the record?
I'm using Miles Johnson's uploader plugin for uploading files.
Here's the code within the form:
<?php
if (!empty ($slider ['Slider']['bgImg']));
echo $slider ['bgImg'];
else
echo $form->input('file', array('type' => 'file'));
?>
This logic should be in the controller, strictly speaking.
In my apps with files and edit capabilities, I will show a file field and a link/thumbnail of the image in question on the edit form.
my approach uses my own uploader, so your results may vary, but essentially:
if (!empty($this->data)) {
$file_object = $this->data['Listing']['featured_image'];
$image_data=$this->Upload->do_upload($file_object, 'img/uploads/');
if($image_data){
$this->data['Listing']['featured_image'] = $image_data['name'];
} else {
unset($this->data['Listing']['featured_image']);
}
$this->Listing->save($this->data)
and in my upload component, I have this:
public function do_upload($file_object, $location='img/uploads/') {
/**
* No file was uploaded
*/
if($file_object['error']==4) {
return false;
}
// carry on uploading
So essentially; I pass my upload component the $file_object, which is from the form. I then do a simple test, using the default set of error codes to see if the file is empty (4). If it's empty, I return false. (you could return errors etc, but this was my approach).
after the upload call, I check the return value, and if the file was uploaded successfully, I can then set the field in my model. (the file name, in my case) - you may want to store the path as well, for instance.
If it's false - it means there was no file; so I unset the value from the array.
Because the field does not exist in the array, cake will not attempt to overwrite existing data - it simply ignores it; allowing the old value to stay untouched.

Resources