How to load datafixtures with files in symfony3 - image

I want to load datafixtures with DoctrineFixturesBundle with images but I don't know how to make it work.
I tried this one :
public function load(ObjectManager $manager)
{
$imageGrabTail = new Image();
$imageGrabTail->setTrick($this->getReference('grab-tail'));
$imageGrabTail->setUpdatedAt(new \DateTimeImmutable());
$file = new UploadedFile($imageGrabTail->getUploadDir() . '/63.jpeg', 'Image1', null, null, null);
$imageGrabTail->setFile($file);
$manager->persist($imageGrabTail);
$manager->flush();
}
My method getUploadDir():
public function getUploadDir()
{
return 'uploads/img';
}
But I have an error :
[Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException]
The file "uploads/img/63.jpeg" does not exist
My file 63.jpeg exists on this folder.
Is there someone who can explain to me why it's not working ?
Thanks !

This object UploadedFile is for file that are upload through a request. Here there is no request, so you have to use get_file_contents() function in order to access your data.
Try this function and tell us the result of it.

To solve it I've just followed this thread
How would you add a file upload to a Symfony2 DataFixture?

Related

File `{$path}` does not exist error in media library laravel

Can I save image for 2 tables in db with same file request?
in controller
$combo = new Combo;
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$combo->addMediaFromRequest('image')->toMediaCollection('combos');
}
$menuItem = new MenuItem;
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$combo->addMediaFromRequest('image')->toMediaCollection('menu-items');
}
Can someone help me fix this code to save it? Thank you very much for your help

Laravel queues Call to a member function storeAs() on string

I am trying to store heavy image through the laravel queue as follows because i dont want my user to wait until file gets stored:
This code is in controller
if($request->hasfile('featuringPhoto'))
{
$prefixFilename = date("Y-m-d-H-i-s");
$coverFilename = $prefixFilename.$request->featuringPhoto->getClientOriginalName();
ProceessFiles::dispatch($request->featuringPhoto->getRealPath(),$coverFilename);
}
else
{
$coverFilename4 = NULL;
}
Below code is in job
protected $files, $filename;
public function __construct($files,$filename)
{
$this->files= $files;
$this->filename = $filename;
}
public function handle()
{
if($this->files){
$coverFilename = $prefixFilename.$this->filename;
$img = file_get_contents($this->files);
$img->storeAs('images/photosUploadedByUser', $coverFilename, 'public');
}
}
It Gives me an error saying Call to a member function storeAs() on string
I tried this solution from stack but didnt work
Any suggestion will be appreciated.Thank you.
I think it is wrong to assume you are gonna save a lot time by executing the save operation in a queue, also because you are already fetching it from the web server. Queues will with scaling often be moved to worker servers and with this approach this will not work.
In the spirit of the question, stackoverflow and to explain to you what is not working. file_get_contents() returns the content of the file as a string. So to fix your problem, you should just store the results of that. You obviously can not call methods on strings.
$coverFilename = $prefixFilename.$this->filename;
$img = file_get_contents($this->files);
Storage::put('images/photosUploadedByUser/' . $coverFilename, $img);

delete old image while updating the field

I am doing CRUD operations for images. when I am doing the update operation image gets updated but the old image doesn't delete. The following code updates only the file name in the database but I need to remove the old image from the destination folder too while updating else the folder size will become too large. Any ideas would be great. Here my code.
<input type="file" name="profileimage" value="{{ isset($editabout) ? $editabout->profileimage : '' }}" class="custom-file-input" id="exampleInputFile">
Here is my controller
public function update(updateAboutusProfile $request, AboutUs $about)
{
$data=$request->only(['profiletext']);
if($request->hasFile('profileimage')){
$profileimage = $request->profileimage->store('aboutus', 'public');
$oldimage = $about->profileimage;
Storage::delete($oldimage);
$data['profileimage'] = $profileimage;
}
$about->update($data);
toastr()->success('Profile updated successfully');
return redirect(route('about.index'));
//
}
What can be the error I need to resolve,
thank you
Try setting the disk you used to store the file
Storage::disk('public')->delete($oldimage);
Delete image from your server, you have to reference location of file in directory server, means you could not reference by url link to delete it.
Laravel file is locate in public folder.
Example: your files are located in public/images
$image_path = "/images/filename.ext"; // Value is not URL but directory file path
if(File::exists($image_path)) {
File::delete($image_path);
}
Enjoy ;)
You should save the path of the file to the database and then simply remove it using \Storage::delete() facade.
Store image using hash name
if($request->hasFile('profileimage')){
$store_path = 'aboutus';
$profileimage = $request->profileimage->store($store_path,'public');
$oldimage = $about->profileimage;
$file_address = $store_path.'/'.$request->profileimage->hashName();
\Storage::disk('public')->delete($oldimage);
$data['profileimage'] = $file_address;
}
Store image using original name
if($request->hasFile('profileimage')){
$store_path = 'aboutus';
$profileimage = $request->profileimage->storeAs($store_path, $uploadedFile->getClientOriginalName(),'public');
$oldimage = $about->profileimage;
$file_address = $store_path.'/'.$request->profileimage->getClientOriginalName();
\Storage::disk('public')->delete($oldimage);
$data['profileimage'] = $file_address;
}
Delete the old image while updating
public function update(Request $request, $id)
{
// other updates
if ($request->hasFile('image')) {
//delete old image if exist
if (File::exists(public_path($oldImage))) {
File::delete(public_path(oldImage));
}
//new image process
}
}

Deleting file which was uploaded with $request->file->store

For some reason I am not able to delete a file I upload with $request->file->store.
I tried \File::delete, \File::Delete, \Storage::Delete, and \Storage::delete. I want to avoid using unlink as that is not the laravel way.
May you please help me?
This is my code:
if ($user->avatar_path) {
// delete old one
\File::delete(app_path().$avatar_path);
}
$avatar_path = $request->file('avatar')->store('uploads/users/'.$user->id.'/avatar', 'public');
$user->update($request->except('avatar') + ['avatar_path' => $avatar_path]);
We see here that I check if there is an old avatar, and then I want to delete it. Then I upload the next one and update the database with the new path.
Finally solved this. I had to use Storage::disk('public')->delete($old_avatar_path);. Final code:
if ($request->has('avatar')) {
$old_avatar_path = $user->avatar_path;
$avatar_path = $request->file('avatar')->store('avatars', 'public');
$user->update($request->except('avatar') + ['avatar_path' => $avatar_path]);
if ($old_avatar_path) {
// delete old one
Storage::disk('public')->delete($old_avatar_path);
}

MvcRazorToPdf How to download file using ajax call? MVC

I tried to use MvcRazorToPdf, i can able to generate mail merge letters. My aim is after downloading file i want to return json message from controller. how to do?
Controller: [how to convert the below lines to generate pdf and download without return]
return new PdfActionResult(pdfres, (writer, document) =>
{
document.SetPageSize(new Rectangle(PageSize.A4));
document.NewPage();
})
{
FileDownloadName = "ElanWasHere.pdf"
};
I appreciate your help. thanks

Resources