How to get original name of the image? - laravel-8

Hello guys How to get the original client name of an image in the collection?
I have this new code to upload more than three images
public function store(){
$action = '';
$data = $this->validate([
'album_title' => 'required',
'photos.*' => 'image|max:2000', // 2MB Max
]);
//New Code with collections
$filenames = collect($this->photos)->map->store('photos');
if($this->galleryId){
Gallery::find($this->galleryId)->update($data);
$action = 'edit';
}else{
// Gallery::create($data);
Gallery::create([
'album_title' => $this->album_title,
'photos' => $filenames->implode(','),
]);
$action = 'store';
}
$this->emit('showEmitedFlashMessage', $action);
$this->resetInputFields();
$this->emit('refreshParent');
$this->emit('closeGalleryModal');
}
but it save something like this
photos/xTNV0cQ7YzpyUsJ6OdrpQirwfCPdzDtKJAKoWUFm.jpg,photos/32PkRR9bFhM7BqAy9OWG05m5fe53PUXtidEMhlnn.jpg,photos/Dyps8wCsga0zdALGsKEOS9jcYSNshnixw4UWzhEL.jpg
What I want is to save the original name of the images.

What I usually do is set the file name to a uuid for storage in the system, but retain the original filename in the database so that you can rename it for downloads if you want. To get the original file name, you need to look at the post data for that.
like so:
$this->active_document->original_filename = $this->uploaded_file->getClientOriginalName();
More info here in the docs: https://laravel.com/docs/8.x/filesystem#file-uploads

Related

Trying to find a file that doesnt exist Yii2

Im trying to create an email function that sends a file attached.
I've created the function and added attach() with the route of file I want to attach as follows:
public function newMonthlyBillingRegister(Clinic $clinic, ClinicMonthlyBilling $clinicMonthlyBilling)
{
$countryAdmins = $clinic->findCountryAdmins($clinic->country->id);
$adminEmails = [];
foreach ($countryAdmins as $admin) {
$adminEmails[$admin->email] = $admin->fullName;
}
/** #var BaseMailer $mailer */
$mailer = Yii::$app->mailer;
$mailer->htmlLayout = 'layouts/standard_html';
//$mailer->htmlLayout = 'layouts/invoice_html';
// $mailer->textLayout = 'layouts/invoice_text';
try{
return $mailer
->compose(
['html' => 'newMonthlyBillingRegister-html', 'text' => 'newMonthlyBillingRegister-text'],
[
'clinic' => $clinic,
'clinicMonthlyBilling' => $clinicMonthlyBilling,
]
)
->setFrom([Yii::$app->params['supportEmail'] => Yii::t('app', 'support-email-name', ['appName' => Yii::$app->name])])
->setTo($adminEmails)
->setSubject(Yii::t('app', '[CLINIC][BACKOFFICE] A clinic has upgraded its information in Guarantee Fund Modal.'))
->attach('app/media/uploads/clinic/monthly-billing/'.$clinicMonthlyBilling->trimestralBillingFile)
->queue() && YII_DEBUG && Yii::$app->mailqueue->process();
}
catch(\Exception $e){
LogService::getLogger()->error("Error while sending mail.","EmailService:newMonthlyBillingRegister()",$e->getMessage());
}
}
It gave me an error, but the problem comes when I delete attach() from my code, reverting all my changes on this email function, and when I try to send again the email the following message appears:
PHP Warning: fopen(app/media/uploads/clinic/monthly-billing/user_1_clinic_1_trimestral_billing_file_2019_05_09_08_22_35): failed to open stream: No such file or directory in /app/vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/FileByteStream.php on line 142
The user_1_clinic_1_trimestral_billing_file_2019_05_09_08_22_35 file doesnt exist in app/media/uploads/clinic/monthly-billing/ folder and even in my database
...what's wrong?¿ I know is in my local but I dont know the reason of trying to search a file that doesn't exist and I didn't code anything to search it,
Anyone can help? Im using PHPStorm,
Thanks a lot for your attention!
I had to clean my email queue and it worked!! Thanks a lot Insane Skull for your help ;)

Snappy / WKHTMLtoPDF - How to change the save folder

I'm using Laravel with the snappy wrapper. It is all working except that it saves the PDF's into the public folder. I want it to go to a different folder. There is nothing obvious on the snappy git site, nor in the config. And the wkhtlptopdf docs are very sparse imho. How do I change the $pdf->save() statement so it goes where I want it to go ?
My PDF is generated by laravel like this:
if( $email == 'email'){
$quotation = $this->quotation->get_PdfQuote($ref);
$pdf = PDF::loadView('quotations/pdf_quotation',compact('quotation') );
$pdf->save($ref.'.pdf'); //THIS SAVES INTO THE PUBLIC FOLDER.
$title = 'Your Quotation';
$firstname = $customer['firstname1'];
$pathtoFile = '/var/www/auburntree/public/'.$ref.'.pdf';
Mail::send('emails.quotation', ['title' => $title, 'firstname'=>$firstname ], function ($m) use($customer,$pathtoFile) {
$m->from('myemail#gmail.com', 'Auburntree');
$m->to($customer['email1'],($customer['firstname1'] . $customer['lastname1']))->subject('Your Quotation');
$m->attach($pathtoFile);
});
Flash::success('The Quote Has Been Saved. An Email has ben sent to the customer ');
return redirect ('quotes');
} else
Ok, I hope this helps someone else.
$quotation = $this->quotation->get_PdfQuote($ref); //pulled in from DB
$pdf = PDF::loadView('quotations/pdf_quotation',compact('quotation') );
$filename = base_path('public/pdf/'.$ref.'.pdf');
$pdf->save($filename);

TYPO3 6.2 - how to create FileReference in frontend (FE)?

I have the hypothetical Zoo extension in which I've Animal model with photo field and FrontEnd (FE) plugin with typical CRUD actions. photo field is typical FAL's FileReference and it works perfectly in backend (BE) with common TCA IRRE config.
I'm able to successful upload the file to the storage, it's visible in the Filelist module, and I can use it in BE during my Animal editing, anyway I can't create FileReference within my FE plugin.
My current approach looks like this:
/**
* #param \Zoo\Zoo\Domain\Model\Animal $animal
*/
public function updateAction(\Zoo\Zoo\Domain\Model\Animal $animal) {
// It reads proper uploaded `photo` from form's $_FILES
$file = $this->getFromFILES('tx_zoo_animal', 'photo');
if ($file && is_array($file) && $file['error'] == 0) {
/** #type $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
$storageRepository = GeneralUtility::makeInstance('\TYPO3\CMS\Core\Resource\StorageRepository');
$storage = $storageRepository->findByUid(5); // TODO: make target storage configurable
// This adds uploaded file to the storage perfectly
$fileObject = $storage->addFile($file['tmp_name'], $storage->getRootLevelFolder(), $file['name']);
// Here I stuck... below line doesn't work (throws Exception no. 1 :/)
// It's 'cause $fileObject is type of FileInterface and FileReference is required
$animal->addPhoto($fileObject);
}
$this->animalRepository->update($animal);
$this->redirect('list');
}
anyway attempt to create reference by this line throws exception:
$animal->addPhoto($fileObject);
How can I resolve this?
Checked: DataHandler approach (link) won't work also, as it's unavailable for FE users.
TL;DR
How to add FileReference to Animal model from existing (just created) FAL record?
You need to do several things. This issue on forge is where I got the info, and some stuff is taken out of Helmut Hummels frontend upload example (and the accompanying blogpost) which #derhansen already commented.
I'm not entirely sure if this is everything you need, so feel free to add things. This does not use a TypeConverter, which you should probably do. That would open further possibilities, for example it would be easily possible to implement deletion and replacement of file references.
You need to:
Create a FAL file reference object from the File object. This can be done using FALs resource factory.
Wrap it in a \TYPO3\CMS\Extbase\Domain\Model\FileReference (method ->setOriginalResource)
EDIT: This step is unnecessary as of TYPO3 6.2.11 and 7.2, you can directly use the class \TYPO3\CMS\Extbase\Domain\Model\FileReference.
But, because the extbase model misses a field ($uidLocal) in 6.2.10rc1, that won't work. You need to inherit from the extbase model, add that field, and fill it. Don't forget to add a mapping in TypoScript to map your own model to sys_file_reference.
config.tx_extbase.persistence.classes.Zoo\Zoo\Domain\Model\FileReference.mapping.tableName = sys_file_reference
The class would look like this (taken from the forge issue):
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference {
/**
* We need this property so that the Extbase persistence can properly persist the object
*
* #var integer
*/
protected $uidLocal;
/**
* #param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource
*/
public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource) {
$this->originalResource = $originalResource;
$this->uidLocal = (int)$originalResource->getUid();
}
}
Add this to the TCA of the image field, in the config-section (adapt to your table and field names of course):
'foreign_match_fields' => array(
'fieldname' => 'photo',
'tablenames' => 'tx_zoo_domain_model_animal',
'table_local' => 'sys_file',
),
EDIT: Use \TYPO3\CMS\Extbase\Domain\Model\FileReference in this step if on TYPO3 6.2.11 or 7.2 or above.
So at the end add the created $fileRef instead of $fileObject
$fileRef = GeneralUtility::makeInstance('\Zoo\Zoo\Domain\Model\FileReference');
$fileRef->setOriginalResource($fileObject);
$animal->addPhoto($fileRef);
Don't tell anyone what you have done.
Here is the complete function to upload file in TYPO3 using FAL and create filereference
/**
* Function to upload file and create file reference
*
* #var array $fileData
* #var mixed $obj foreing model object
*
* #return void
*/
private function uploadAndCreateFileReference($fileData, $obj) {
$storageUid = 2;
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
//Adding file to storage
$storage = $resourceFactory->getStorageObject($storageUid);
if (!is_object($storage)) {
$storage = $resourceFactory->getDefaultStorage();
}
$file = $storage->addFile(
$fileData['tmp_name'],
$storage->getRootLevelFolder(),
$fileData['name']
);
//Creating file reference
$newId = uniqid('NEW_');
$data = [];
$data['sys_file_reference'][$newId] = [
'table_local' => 'sys_file',
'uid_local' => $file->getUid(),
'tablenames' => 'tx_imageupload_domain_model_upload', //foreign table name
'uid_foreign' => $obj->getUid(),
'fieldname' => 'image', //field name of foreign table
'pid' => $obj->getPid(),
];
$data['tx_imageupload_domain_model_upload'][$obj->getUid()] = [
'image' => $newId,
];
$dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
'TYPO3\CMS\Core\DataHandling\DataHandler'
);
$dataHandler->start($data, []);
}
where $filedata =
$this->request->getArgument('file_input_field_name');
And
$obj = //Object of your model for which you are creating file
reference
This example does not deserve a beauty prize but it might help you. It works in 7.6.x
private function uploadLogo(){
$file['name'] = $_FILES['logo']['name'];
$file['type'] = $_FILES['logo']['type'];
$file['tmp_name'] = $_FILES['logo']['tmp_name'];
$file['size'] = $_FILES['logo']['size'];
// Store the image
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
$storage = $resourceFactory->getDefaultStorage();
$saveFolder = $storage->getFolder('logo-companies/');
$newFile = $storage->addFile(
$file['tmp_name'],
$saveFolder,
$file['name']
);
// remove earlier refereces
$GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_file_reference', 'uid_foreign = '. $this->getCurrentUserCompanyID());
$addressRecord = $this->getUserCompanyAddressRecord();
// Create new reference
$data = array(
'table_local' => 'sys_file',
'uid_local' => $newFile->getUid(),
'tablenames' => 'tt_address',
'uid_foreign' => $addressRecord['uid'],
'fieldname' => 'image',
'pid' => $addressRecord['pid']
);
$GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $data);
$newId = $GLOBALS['TYPO3_DB']->sql_insert_id();
$where = "tt_address.uid = ".$addressRecord['uid'];
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_address', $where, array('image' => $newId ));
}

How to store file and retrive it correctly with moodle file api?

i'm programming a mod activity for moodle which load files and show'em to any student who can access to the course.
The problem is that handing files in moodle is damn hard.
this is what i have done so far:
option page with importers
$mform->addElement('filepicker', 'slidesyncmedia', get_string('slidesyncmedia', 'slidesync'), null, array('maxbytes' => $maxbytes, 'accepted_types' => '*'));
$mform->addElement('filemanager', 'slidesyncslides', get_string('slidesyncslides', 'slidesync'), null, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50, 'accepted_types' => array('*') ));
after submit the files are stored in draft
and everything is loaded in another page that save all on db
if ($draftitemid = file_get_submitted_draft_itemid('slidesyncmedia')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_slidesync', 'slidesyncmedia', 0, array('subdirs' => 0, 'maxfiles' => 1));
}
if ($draftitemid = file_get_submitted_draft_itemid('slidesyncslides')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_slidesync', 'slidesyncslides', 0, array('subdirs' => 0, 'maxfiles' => 50));
}
in the end i use again the first page in another place (if files are there, then shows them)
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_slidesync', 'slidesyncslides', '0', 'sortorder', false)) {
// Look through each file being managed
foreach ($files as $file) {
// Build the File URL. Long process! But extremely accurate.
$fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
echo $fileurl;
}
} else {
echo '<p>Please upload an image first</p>';
}
this make an url but if clicked moodle says that file does not exist
mysite.com/pluginfile.php/53/mod_slidesync/slidesyncslides/0/Koala.jpg
in the db the file are correctly saved!!!
53 mod_slidesync slidesyncslides 0 / Koala.jpg
what i'm missing?
thanks
A long time passed but I was working on a plugin and had the same problem.
I manage to solve it.
To provide the file you need to create the:
function MYPLUGIN_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array())
Here is the example function: https://docs.moodle.org/dev/File_API#Serving_files_to_users
Remember change the last call to send_file to send_stored_file in Moodle 2.3+

symfony setPostValidator with sfValidatorFile

I am going through an issue to setup a file upload validator on callback.
I want to achieve this:
I have a form, where user choose the type of the file they are uploading and upload the file.
So I want to set the validator to handle images in case they chose "img" as type, and pdf if they chose "pdf".
Moreover I want to specify the mime type and path and validatedFileClass according to the type.
I tried this.. but i can't get it to work
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(array('callback' => array($this, 'validateUploadedFiles')))
);
the function:
public function validateUploadedFiles($form_validator, $values){
$this_year = date("Y");
$this_month = date("m");
$basic_validator = array(
'required' => true,
'path' => sfConfig::get('sf_upload_dir').'restaurant/media/'.$this_year.'/'.$this_month.'/'
);
$doc_validator = $video_validator = $img_validator = $pdf_validator = $basic_validator;
$pdf_validator['mime_types'] = array ('application/pdf');
$doc_validator['mime_types'] = array ('application/msword', 'application/vnd.openxmlformats');
$img_validator['mime_types'] = 'web_images';
//$img_validator['validated_file_class'] = 'imgHandling';
$video_validator['mime_types'] = array('video/mpeg', 'video/3gpp', 'video/mp4', 'video/quicktime');
switch( $values['type'] ):
case 'pdf' : $validator = $pdf_validator; break;
case 'img' : $validator = $img_validator; break;
case 'word' : $validator = $doc_validator; break;
case 'video' : $validator = $video_validator; break;
endswitch;
//$form->getValidatorSchema()->offsetUnset('url') ;
//print_r($validator_fields);
$validator = new sfValidatorFile( $validator );
$validator_schema = new sfValidatorSchema();
$validator_schema->offsetSet('url', $validator);
//$validator_fields = $form->getValidatorSchema()->getFields();
$schema = parent::getValidatorSchema();
$schema->offsetUnset('url') ;
$schema->offsetSet('url', $validator);
// $path = sfConfig::get('sf_upload_dir').'restaurant/media/'.$this_year.'/'.$this_month.'/';
// $values['url'] = new sfValidatedFile( $values['url']['name'], $values['url']['type'], $values['url']['tmp_name'], $values['url']['size'] , $path);
//TODO get this url value run through the new added validator
//$values['url'] = $validator_schema->clean(array( 'url' => $values['url'] ));
return $values;
}
the problem i am facing is that , this function receives the url value as array, and even if I update the validators schema, it's not validating the url and keeps on sending it as array to the object saving method.
So how to make something like
url.validateFile() from inside this function
Not sure about the best solution, but I'd prefer to split validation process into 2 parts:
Validate mime type
If mime is ok, then validate URL
Or vise versa, your choice.
The glue would be sfValidatorAnd.
Did I understand you right?