Download all files in laravel media library without collection name - laravel

I'm trying to download all the uploads together under one user. If one user has uploaded under multiple collections, I want to download all uploads under all collections in one click as a zip file.
I tried without the collection name in getMedia function. But it's not getting my result.
$user = User::where('id',auth()->user()->id)->first();
$downloads = $user->getMedia();
return MediaStream::create('my-files.zip')->addMedia($downloads);
How can I download all files without the collection name in the media library in one click as a zip file?

I got the solution.
With collection name
$user = User::where('id',auth()->user()->id)->first();
$downloads = $user->getMedia('passport');
return MediaStream::create('myfiles.zip')->addMedia($downloads);
Without collection name
$user = User::where('id',auth()->user()->id)->first();
$downloads = ModelsMedia::where('model_id',$user->id)->get();
return MediaStream::create('myfiles.zip')->addMedia($downloads);

Related

Laravel: How to prevent users loading files from outside the base path

I have a Laravel website and I have several routes that load the contents of images from Storage. I do this using the following code:
public function show_image($name) {
echo Storage::disk('images')->get($name);
}
I want to prevent users being able to set name to something like ../../../error.log. So I don't want users to escape the Storage directory. I have a few ideas on how to accomplish this however I want to know is there a best practice?
If you need just file name, not location, disallow them from inputting folder of any kind. Just cut the string on /.
end(preg_split("#/#", $name));
When you need to allow some folders and all of the contents, check the folder name, subfolder name, etc.
You could either keep a registry/index of the uploaded images, and only allow the user to show a image from that registry (e.g. an images database table).
Or you could do a scan of the directory, that you are allowing files from, and make sure, that the requested file is in that list.
public function show_image($name) {
$files = Storage::disk('images')->files();
if (! in_array($name, $files)) {
throw new \Exception('Requested file not found');
}
echo Storage::disk('images')->get($name);
}
(code untested)

Laravel Mediable - deleting media along with physical file

Using Laravel Mediable and I'm trying to figure out the best way to delete an individual file. Say there's a list of files when I'm viewing the parent Model, and I click a delete icon to make an ajax request to delete file (which should remove both the corresponding Media object and the physical file...)
This works:
$path_parts = pathinfo($request->filename);
$attachment = Media::where('directory', $folder)
->where('filename', $path_parts['filename'])
->where('extension', $path_parts['extension'])
->first();
$attachment->delete();
but this deletes only the database row and not the physical file itself:
$attachment = Media::where('id', $request->fileid);
$attachment->delete();
I'd prefer deleting the file via the id because its unique but wondering what I'm missing...
You must delete the file also using this code
File::delete('path/to/'.$request->filename);
=updated=
The problem on the second block is just need to add ->first() at the end of the where clause

How to retrieve attachement file From Custom list sharepoint2010?

I have create custom list..and submit Form data onto list by Visual webpart.but i have doubts when i have attach on the attachments of list.
now a time to retrieve Data from custom list but how can i retrieve attachement file from list and easy to download by End user???
Plz anyone guard me
SPFolder folder = web.Folders["Lists"].SubFolders[list.Title].SubFolders["Attachments"].SubFolders[listitem.ID.ToString()];
Then you can do:
foreach (SPFile file in folder.Files)
{
// Do what you want to do with the file
}

Drupal 7 Importing images from a csv file

I have written a custom module to import users from a csv file into a drupal 7 database.
The csv file has a field for the users avatar that references an image in a directory.
The issue I'm having is with attaching the images to the user. So the image gets saved correctly in the pictures directory, the record is added the file_managed table, and the file id is added to the user's record. However when I go to edit the user via the drupal interface, the picture does not appear on that page.
The code I've got is below, any help would be awesome!
$userobj = user_load(1);
$file_temp = file_get_contents('/avatars/'.$importfile);
$file_temp = file_save_data($file_temp, 'public://pictures/' . $filename, FILE_EXISTS_RENAME);
$userobj->picture->fid = $file_temp->fid;
$userobj->status = 1;
user_save((object) array('uid' => $record->uid), (array) $userobj);
best way to get file path in d7 is
$path = file_default_scheme() . '://' ;
the path should now look something like public://
i had a mare saving in the users pictures folder but found it was a file permissions thing and had to chmod the folder

drupal_execute populating image field

How to populate image field value with drupal_execute.
for ex my content type (test) has two additional fields
1. photo (image filed),
2. phid (text field)
for phid $form_state['values']['field_phid'][0]['value'] ='14'; . how to populate photo which is image field type
If the file is already uploaded to Drupal and has a file ID (fid) then you can just do
$form_state['values']['field_image_filed'][0]['fid'] = 17; //where 17 is the Drupal file ID of the file you want input
If the file isn't already uploaded it's a lot trickier. You'll first need to programmatically create the file. I can't walk you through it off-hand but a good place to look for a template as to how it should be done is the file_service_save() function in the Services module's file_service.inc:
http://drupalcode.org/viewvc/drupal/contributions/modules/services/services/file_service/file_service.inc?revision=1.1.2.7.2.3&view=markup&pathrev=DRUPAL-6--2-2
To be clear: I'm not saying you'll use file_service_save() to accomplish the upload, but that that code shows you what needs to be done. It will show you how to save the file to the server using file_save_data(), record the file to the Drupal "files" table, then call hook_file_insert to notify other modules that a file's been saved.
i found the solution as below . i dont know pros and cons but it works fine for me.
$image = "*******/test.jpg";
$field = content_fields('field_img', 'img_test');
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
$files_path = filefield_widget_file_path($field);
$form_state['values']['field_img'][]= field_file_save_file($image, $validators, $files_path, FILE_EXISTS_REPLACE);

Resources