Drupal 7 Importing images from a csv file - image

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

Related

how to store pdf file with file_get_contents - note - the pdf will be generated runtime based on url parameters

I'm using stripe to get invoice
I need to store this invoice in my public storage folder - I'm using laravel
stripe does not return pdf file path like this - http://localhost/app/invoice.pdf
instead of that
they provide a url like this - https://pay.stripe.com/invoice/acct_1sflasjfmsklagbs/test_alkfnsajklgdbnajkgsbnajkfbsuayasgtjbwq8tuy23690q8319qr8busajvbssavkjsabngukwjqhr82rhadsawoilafkn/pdf?s=ap
so when you enter above url - it generates a pdf dynamically
I tried this and it works if you have direct path of pdf file
$contents = file_get_contents('http://localhost/app/invoice.pdf');
\Storage::disk('public')->put('filename.pdf', $contents);
but below given code does not work
$contents = file_get_contents('https://pay.stripe.com/invoice/acct_1sflasjfmsklagbs/test_alkfnsajklgdbnajkgsbnajkfbsuayasgtjbwq8tuy23690q8319qr8busajvbssavkjsabngukwjqhr82rhadsawoilafkn/pdf?s=ap');
\Storage::disk('public')->put('filename.pdf', $contents);
anyone can please assist here?

Download all files in laravel media library without collection name

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);

Remove "/public" from file path in Laravel when linking public directory

I use Laravel of 5.7.25 version.
I have a symbolic link from public/storage to /storage/app/public.
I'm storing files in /storage/app/public/place-images directory.
I keep the path of stored file in table files which keeps all stored files. The file path would be public/images/some_hash.jpg.
Now I made a file resource, which is used when I'm getting files from my API. The file path retured from api equals public/images/some_hash.jpg. But instead I need it to be images/some_hash.jpg. However, in the table I prefer to keep real path of the file related to the storage folder. After all, I can keep files in AWS or somewhere else.
As far as I understand storage is the root of my disk. The $file->store() method includes public part of the file path.
I end up doing something like this:
// This is ImageResource file, Image model has File relation. One Image has one File
// TODO: Dirty hack
$path = $this->file->path; // This equals 'public/place-images/hash.jpg'
// Removing 'public' part
$charIndex = strpos($path, '/');
$path = substr($path, $charIndex + 1);
return [
'id' => $this->id,
'original_name' => $this->file->original_name,
url' => asset('storage/' . $path) // now we have /storage/place-images/some_hash.jpg which is correct
];
Is there anyway to avoid this dirty hack? I think I'm missing something obvious
storage is not the root of your disk. You can set the root for a disk in config\filesystems.php.
Default root for the public disk defined in config/filesystems.php is /storage/app/public so just store place-images/hash.jpg without public.
If you want to keep files from different disks in one table just add a field with a disk name to this table.

How to use storage_path() to view an image in laravel 4

I'm using storage_path() for storing uploaded images, but when I use it is pointing wrong on my page.
I use it like this {{ $data->thumbnail }} where $data came from the database and thumbnail comes as the string which used storage_path
Let us take a look at the default L4 application structure:
app // contains restricted server-side application data
app/storage // a writeable directory used by L4 and custom functions to store data ( i.e. log files, ... )
public // this directory is accessible for clients
If I were you, I would upload the file to the public directory directly:
Store image here: public_path() . 'img/filename.jpg'
Save the 'img/filename.jpg' in database
Generate the image URL with url('img/filename.jpg') => http://www.your-domain.com/img/filename.jpg
Hope this helps.
The storage_path function returns the path to the storage folder, which is inside the app folder --and outside the public folder-- so it's not directly accessible from the client, that's why your images are not being displayed. You can move them to the public folder path, or you could use a custom controller to handle the image requests, read the image from the storage folder and return the value.

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