$request->validate([
'title' => 'required|unique:latests|max:255',
'description' => 'required',
'image' => 'required','mimes:jpg,png,jpeg', 'max:5048']);`
if($request->file('image')){
$image = $request->file('image');
$image_gen = hexdec(uniqid()). '.'. $image->getClientOriginalExtension();
Image::make($image)->resize(322, 500)->save(public_path('upload/image-folder/'. $image_gen));
$save_url = 'upload/image-folder'. $image_gen;`
}
$latest = new Latest();
$latest->title = $request->title;
$latest->description = $request->description;
$latest->$image = $save_url;
$latest->save();`
Here is the image for error column in database:
I've tried also this one :
'local' => ['driver' => 'local', 'root' => public_path(),
Unfortunately it didn't work i don't
Related
Please am trying to upload 3 different images using the code below. How do I get each of the methods that generates the unique image names to run only when their respective request fields have data or is not empty. thus the form submit should not try generating any image name for afile field when that particular file field is empty.
My update controller function
public function update(Request $request, Product $product)
{
$image = $request->file('primary_image');
$name_gen = md5(rand(1000, 10000)).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(523,605)->save('upload/products/'.$name_gen);
$save_url = 'upload/products/'.$name_gen;
$image_1 = $request->file('image_1');
$name_gen = md5(rand(1000, 10000)).'.'.$image_1->getClientOriginalExtension();
Image::make($image_1)->resize(523,605)->save('upload/products/'.$name_gen);
$save_url_1 = 'upload/products/'.$name_gen;
$image_2 = $request->file('image_2');
$name_gen = md5(rand(1000, 10000)).'.'.$image_2->getClientOriginalExtension();
Image::make($image_2)->resize(523,605)->save('upload/products/'.$name_gen);
$save_url_2 = 'upload/products/'.$name_gen;
Product::insert([
'name' => $request->name,
'category' => $request->category,
'price' => $request->price,
'description' => $request->description,
'status' => $request -> status,
'estimated_delivery_time' => $request->estimated_delivery_time,
'available_quantity' => $request->available_quantity,
'colors' => $request->colors,
'supplier_name' => $request->supplier_name,
'supplier_phone' => $request->supplier_phone,
'video_description' => $request->video_description,
'primary_image' => $save_url,
'image_1' => $save_url_1,
'image_2' => $save_url_2,
]);
$notification = array(
'message' => 'Product updated successfully',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
Thanks so much for taking time to review my code
Since you are doing the same name generation process all through, you can use an array and do a foreach loop with an if condition like this:
$my_array = [$request->file('primary_image'), $request->file('image_1'), $request->file('image_2')];
foreach($my_array as $item) {
if($item) {
$image = $item;
$name_gen = md5(rand(1000, 10000)).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(523,605)->save('upload/products/'.$name_gen);
$save_url = 'upload/products/'.$name_gen;
}
}
Now this will only generate names for images that are not empty.
UPDATE:
For the insert functionality, I would assume your table fields for the images can have null values, so this doesn't throw an error. Now instead of the code above, do this:
$my_array = [$request->file('primary_image'), $request->file('image_1'), $request->file('image_2')];
$insert_array = [];
foreach($my_array as $item) {
$save_url = '';
if($item) {
$image = $item;
$name_gen = md5(rand(1000, 10000)).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(523,605)->save('upload/products/'.$name_gen);
$save_url = 'upload/products/'.$name_gen;
}
array_push($insert_array, $save_url);
}
Now for your insert query, do this:
Product::insert([
'name' => $request->name,
'category' => $request->category,
'price' => $request->price,
'description' => $request->description,
'status' => $request -> status,
'estimated_delivery_time' => $request->estimated_delivery_time,
'available_quantity' => $request->available_quantity,
'colors' => $request->colors,
'supplier_name' => $request->supplier_name,
'supplier_phone' => $request->supplier_phone,
'video_description' => $request->video_description,
'primary_image' => $insert_array[0],
'image_1' => $insert_array[1],
'image_2' => $insert_array[2],
]);
This would work.
Hi everyone i need some help i work a program called file upload , but i dont get it what happen to the file i upload in laravel. in public folder there's a name called php4CA1.tmp every time i upload a pdf file please can anybody help me ? what is the meaning of this php4CA1.tmp or my code is wrong?
$request->validate([
'cover' => 'required',
'book' => 'required',
'name' => 'required',
'description' => 'required',
'price' => 'required|numeric'
]);
$image = $request->file('cover');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'),$new_name);
$book = $request->file('book');
$new_book_name = rand() . '.' . $book->getClientOriginalExtension();
$book->move(public_path('books'),$book);
$data = array(
'cover' => $new_name,
'book' => $new_book_name,
'name' => $request->name,
'description' => $request->description,
'price' => $request->price
);
Book::create($data);
Hi friend please follow as below you are passing the file instead of file name,
$book = $request->file('book');
$new_book_name = rand() . '.' . $book->getClientOriginalExtension();
$response = $book->move(public_path('books'),$new_book_name );
You are passing $book in
$book->move(public_path('books'),$book);
Use this:
$book = $request->file('book');
$new_book_name = rand() . '.' . $book->getClientOriginalExtension();
$book->move(public_path('books'),$new_book_name);
I´m trying to upload an image from my server to another one using an API.
I´ve uploaded to my server using native Storage facade and then, call the Guzzle function to upload the file to the other server.
The problem is that I can´t get the local of the file. Let´s see some code:
public function saveImage(Request $request)
{
// return $request;
$consorciado = Consorciado::where('id', '=', $request->consorciado_id)->first();
$documento = Documento::where('doc_id', '=', $request->id)->first();
$documento = new Documento();
$image = request()->file('f');
$documento->consorciado_id = $consorciado->id;
$documento->doc_id = $request->id;
$documento->tipo = $request->tipo;
$documento->remark = $request->remark;
$documento->idTematica = $request->idTematica;
$documento->field1 = $request->field1;
$documento->field2 = $request->field2;
$documento->field3 = $request->field3;
$documento->field4 = $request->field4;
$documento->field5 = $request->field5;
$filename = $consorciado->id . "_" . $documento->doc_id . "." . $image->getClientOriginalExtension();
$documento->nome = $filename;
// dd($doc);
// $file = fopen(Storage::disk('local') . '/' . file_get_contents($image), 'r');
// dd($file);
$client = new Client([
'base_uri' => 'http://gedocflex.com.br:8003/api/file',
]);
$response = $client->request('POST', 'http://gedocflex.com.br:8003/api/file', [
'multipart' => [
[
'name' => 'idTematica',
'contents' => '201'
],
[
'name' => 'f',
// 'contents' => $filename, file_get_contents($image)
'contents' => fopen('/' . $filename, file_get_contents($image), 'w+')
],
[
'name' => 'field1',
'contents' => '132.626.688-85'
],
[
'name' => 'field2',
'contents' => '7758'
],
[
'name' => 'field3',
'contents' => '404'
],
[
'name' => 'field4',
'contents' => '0'
],
[
'name' => 'field5',
'contents' => 'Marcello Dantas Correia'
],
[
'name' => 'remark',
'contents' => 'Enviando do meu servidor para o GED'
],
]
]);
return $response;
Storage::disk('local')->put('public/' . $filename, file_get_contents($image), 'public');
$documento->save(); //salva no meu banco
}
I´m using php artisan storage:link and the error is:
{message: "fopen(/1_1.jpeg): failed to open stream: No such file or directory",…}
exception: "ErrorException"
file: "/Users/marcellopato/Sites/contemplado/app/Http/Controllers/DocumentoController.php"
line: 234
message: "fopen(/1_1.jpeg): failed to open stream: No such file or directory"
trace: [{function: "handleError", class: "Illuminate\Foundation\Bootstrap\HandleExceptions", type: "->"},…]
As you can see, I get the name of the file, but not its path.
What I am doing wrong?
Anyone?
Thanks in advance!
I am trying to upload multiple images using guzzlehttp client. I have a form in which other type of data('id', 'date','name' e.t.c..) and images are there.
I want to save other data along with image upload through an Api Request.
I am able to save other data, but i am getting problem in uploading images.
In the API i am accessing my image file by
$request->file('images')
but it is showing empty.
My code for calling the API
$images = $request->file('images');
foreach ($images as $image)
{
$body[] = [
'Content-type' => 'multipart/form-data',
'name' => $image->getClientOriginalName(),
'contents' => fopen($image->getRealPath(), 'r')
];
}
$data = $request->all();
$client = new Client();
$response = $client->request('POST', $url, [
'multipart' => $body,
'json' => $data
]);
I'm handling the Api for uploading image below
if ($request->hasFile('images'))
{
$images = $request->file('images');
foreach ($images as $image)
{
$imageRules = [
'file' => 'mimes:png,jpeg,jpg,JPG|max:1024'
];
$imageMessages = [
'file.mimes' => 'One of the images/video is not valid. (only .png, .jpeg, .jpg, .mp4, .x-flv, .x-mpegURL, .MP2T, .3gpp, .quicktime, .x-msvideo, .x-ms-wmv are accepted)',
'file.max' => 'Image size cannot br more than 1 MB'
];
$imageValidator = Validator::make(['file' => $image], $imageRules, $imageMessages);
if ($imageValidator->fails())
{
return response()->json(['success' => false, 'error' => $imageValidator->messages()->first(), 'dashRedirectUrl' => $redirectRoute]);
}
}
$directPath = '/ticket/' . $ticket->id . '/mapping/' . $mappingId . '/images/';
foreach ($images as $image)
{
$option = ['isWatermark' => false];
$imageName = $this->uploadImageToServer($image, $directPath, $option);
$imgInsertData = [
'url' => $imageName,
'title' => $this->getImageTitle($image),
'ticket_mapping_id' => $mappingId,
'type_id' => 1,
];
TicketMappingGallery::create($imgInsertData);
}
}
Note :: My funciton uploadImageToServer() is custom function for uploading the images..
Any help would be appreciated. Drop comment if anything is not clear.
Try this one,
foreach ($image as $img) {
$body[] = [
'name' => 'image[]',
'image_path' => $img->getPathname(),
'image_mime' => $img->getmimeType(),
'image_org' => $img->getClientOriginalName(),
'contents' => fopen($img->getRealPath(), 'r')
];
}
$requestOptions = ['query' => $data, 'multipart' => $body
];
i have implemented multiple product upload and save the details to Database, i need to send the mail with inserted multiple product data to mail
My Controller:
public function store(Request $request)
{
$input = Input::all();
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'trade'; // upload path
$files = Input::file('file');
$length = 6;
$randomString = substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
$trade = Trade::create(array(
'id' => $request->get('id'),
'first_name' => $request->get('first_name'),
'last_name' => $request->get('last_name'),
'address1' => $request->get('address1'),
'address2' => $request->get('address2'),
'zip_code' => $request->get('zip_code'),
'country' => $request->get('country'),
'email' => $request->get('email'),
'phone' => $request->get('phone'),
'bank_name' => $request->get('bank_name'),
'account_number' => $request->get('account_number'),
'iban_number' => $request->get('iban_number'),
'swift_number' => $request->get('swift_number'),
'voucher_code' => $randomString . $request->get('voucher_code'),
));
foreach ($input['purchase_item'] as $k => $v) {
$file = $input['file'][$k];
if (!file_exists($destinationPath)) {
mkdir($destinationPath, 0777, true);
}
$extension = $file->getClientOriginalExtension();
$file_name = time() . '_' . $file->getClientOriginalName();
$fileName = $file_name; // renameing image
$file->move($destinationPath, $fileName);
$timestamp = strtotime($input['purchase_date'][$k]);
$new_date = date('Y-m-d', $timestamp);
$tradeins_products = TradeProduct::create(array(
'tradins_id' => $trade->id,
'product_id' => $input['product_id'][$k],
'application_id' => $input['application_id'][$k],
'brand_id' => $input['brand_id'][$k],
'purchase_date' => $new_date,
'purchase_item' => $input['purchase_item'][$k] ,
'file' => $fileName
));
$data = array(
'first_name' => $request->get('first_name'),
'last_name' => $request->get('last_name'),
'email' => $request->get('email'),
'product_name' => $tradein_product_name[0]['name'],
'address' => $request->get('address1'),
'phone' => $request->get('phone'),
'bank_name' => $request->get('bank_name'),
'account_number' => $request->get('account_number'),
'purchase_date'=>$new_date,
'country_email'=>$country_name[0]['email'],
'country_address1' =>$country_name[0]['address_1'],
'country_address2'=>$country_name[0]['address_2'],
'country_city'=>$country_name[0]['city'],
'country_state'=>$country_name[0]['state'],
'country_zipcode'=>$country_name[0]['zipcode'],
'voucher_code'=>$trade->voucher_code
);
Mail::send('frontend.mail_trade', $data, function($message) use ($data){
$message->to($data['email'], $data['first_name'].' '.$data['last_name'])->subject('Product Trade-in');
return Response::json(array('voucher_code' => $trade- >voucher_code,'first_name'=>$trade->first_name,'email'=>$trade->email,'phone'=>$trade->phone,'address1'=>$trade->address1,'zip_code'=>$trade->zip_code,'country'=>$trade->country,'country_name' => $country_name[0]['name'],'voucher_code1' => $trade->voucher_code,'country_address1' =>$country_name[0]['address_1'],'country_address2'=>$country_name[0]['address_2'],'country_city' =>$country_name[0]['city'],'country_state' =>$country_name[0]['state'],'country_zipcode' =>$country_name[0]['zipcode'], 'country_email' =>$country_name[0]['email'], 'company_name' => $country_name[0]['company_name']));
}
So Above i done inserting two tables, tradin, and tradein products, so tradein products contain multiple product upload and insertion, so i need to show like below on mail
Product Data:
1)product name
-Sony
-Samsung
2)Application Name:
-Handsfree
-Wireless
So how is this possible with using foreach or any other alternate way to do it to show the Data's in mail template,
My Mail Template:
<h3>Cash Back</h3>
<p>Product Name:{{$product_name}} </p>
<p>Name: {{$first_name}} {{$last_name}}
</p>
<p>Address: {{$address}}</p>
<p>Phone:{{$phone}} </p>
<p>Bank: {{$bank_name}}</p>
<p>Account Number:{{$account_number}} </p>
<p>Voucher Code:{{$voucher_code}} </p>
<p>Company Email:{{$country_email}} </p>
<p>Company Address1:{{$country_address1}} </p>
<p>Company Address2:{{$country_address2}} </p>
<p>Company City:{{$country_city}} </p>
<p>Company State:{{$country_state}} </p>
<p>Company Zipcode:{{$country_zipcode}} </p>
<p>Purchase Date: {{$purchase_date}}</p>
Take note that first parameter is your email view, and the second parameter is the data you want to pass to your view. for example:
Mail::send('frontend.mail_trade', $productsData, function($message) use ($data){
// your code
});