Excel File Import using PHPExcel in codeigniter - php

Okay so I am trying to insert my data via using this code and as you can see here everything looks fine but still I'm getting issue like whenever I just insert one row it redirects me back to access denied and data don't get insert while when I make these rows two in excel file it inserts data immediately
if (isset($_FILES['employees_file'])) {
$file = $_FILES['employees_file']['tmp_name'];
$this->load->library('PHPExcel');
$excel = PHPExcel_IOFactory::load($file);
// $sheet = $excel->getSheet(0);
foreach($excel->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
for($row=2; $row<=$highestRow; $row++)
{
$data[] = array(
'first_name' => ($cellValue = $worksheet->getCellByColumnAndRow(0, $row)->getValue()) == "" ? null : $cellValue,
'last_name' =>$worksheet->getCellByColumnAndRow(1, $row)->getValue(),
'enroll_id' =>$worksheet->getCellByColumnAndRow(2, $row)->getValue(),
'nic' =>$worksheet->getCellByColumnAndRow(3, $row)->getValue(),
'job_title_id' =>$worksheet->getCellByColumnAndRow(4, $row)->getValue(),
'join_date' =>$worksheet->getCellByColumnAndRow(5, $row)->getValue(),
'loc_id' =>$worksheet->getCellByColumnAndRow(6, $row)->getValue(),
'supervisor_id' =>$worksheet->getCellByColumnAndRow(7, $row)->getValue(),
'dob' =>$worksheet->getCellByColumnAndRow(8, $row)->getValue(),
'dept_id' =>$worksheet->getCellByColumnAndRow(9, $row)->getValue(),
'email' =>$worksheet->getCellByColumnAndRow(10, $row)->getValue(),
'official_email' =>$worksheet->getCellByColumnAndRow(11, $row)->getValue(),
'phone' =>$worksheet->getCellByColumnAndRow(12, $row)->getValue(),
'address' =>$worksheet->getCellByColumnAndRow(13, $row)->getValue(),
'marital_status' =>$worksheet->getCellByColumnAndRow(14, $row)->getValue(),
'gender' =>$worksheet->getCellByColumnAndRow(15, $row)->getValue(),
'rel_name' =>$worksheet->getCellByColumnAndRow(16, $row)->getValue(),
'rel_relation' =>$worksheet->getCellByColumnAndRow(17, $row)->getValue(),
'rel_contact' =>$worksheet->getCellByColumnAndRow(18, $row)->getValue(),
'employment_status' =>$worksheet->getCellByColumnAndRow(19, $row)->getValue(),
'date_of_confirmation' =>$worksheet->getCellByColumnAndRow(20, $row)->getValue(),
'gross_salary' =>$worksheet->getCellByColumnAndRow(21, $row)->getValue(),
'dept_role' =>$worksheet->getCellByColumnAndRow(22, $row)->getValue(),
);
}
}
// array_shift($data);
$this->SqlModel->insert($data);
}
if ($this->db->affected_rows() > 0) {
redirect('employees/index/success');
} else {
redirect(base_url().'accessdenied');
}

Related

laravel 9 " `C:\XAMPP\tmp\php6CB6`.`tmp`, " issue when uploading image

$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

Skip image validation and creation in laravel update function

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.

how to solve Illuminate\Contracts\Filesystem\FileNotFoundException: File not found at path on FTP storage laravel?

example image response error File not found at path
hey can you help me?
so here I want to view the image file from the ftp server that will be responded to by JS along with the FTP server link
controller example :
$explode = explode('#',$lampiran->lampiran_gambar);
foreach($explode as $row){
if($row == null){
$row1[] = 'null';
}else{
$row1[] = Storage::disk('ftp')->get('/lampiranSurat' . $row);
}
}
if($pegawai_pejabat->jenis_jabatan_id == 1){
return response()->json([
'meta' => [
'code' => 200,
'status' => 'success',
'message' => 'Data Ditemukan',
],
'data_verifikasi' => $verifikasi,
'lampiran_gambar' => $row1,
'pegawai_verif' => $pegawai_verif,
]);
}
example config filesystem :
'default' => env('FILESYSTEM_DRIVER', 'ftp')
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'root' => '/web',
],
config file .env :
FTP_HOST=exampleftpserver.com
FTP_USERNAME=userftp
FTP_PASSWORD=password123
so why is my ftp url not being read in storage?
for image file data already in the database and already in FTP
You're missing a / between your directory name and the filename:
Replace
$row1[] = Storage::disk('ftp')->get('/lampiranSurat' . $row);
With
$row1[] = Storage::disk('ftp')->get('/lampiranSurat/' . $row);

How to attach different picture for each product type

I am building an eCommerce website and I want to add different combinations for the same product.
I use jQuery to add extra input fields as well.
I can successfully insert all data related to product_variation things like color, weight, type, qty but except images.
My database tables: products, product_images, product_variations.
In product_variations table I have field product_variation_image which I want to use for storing image for specific product type. I just don't know how to insert image there for specific product type, yes I know I need to use foreach loop but the problem is I'm already using foreach loop for product variation data, I tried so many different things nesting foreach into foreach but nothing seems to work.
P.S product_images table I'm only use for product basic images also for products which don't have any type.
I will leave my code below... with some comments on where the problem might be.
public function product_store(Request $request)
{
// PRODUCT TYPE VARIABLES //
$productTypes = $request->product_type;
$productColors = $request->product_color;
$productWeight = $request->product_weight;
$productQtys = $request->product_qty;
$productPrices = $request->product_variation_price;
$productTypeImages = $request->file('product_variation_image');
// PRODUCT TYPE VARIABLES ENDS //
// PRODUCT COVER IMAGE //
$productCoverImage = $request->file('product_cover_image');
$productCoverImageNewName = hexdec(uniqid()).'.'.$productCoverImage->getClientOriginalExtension();
Image::make($productCoverImage)->resize(917,1000)->save('upload/products/cover-images/'.$productCoverImageNewName);
$productCoverImageLink = 'upload/products/cover-images/'.$productCoverImageNewName;
// PRODUCT COVER IMAGE ENDS //
if(!empty(implode($productTypes))) {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
]);
// PRODUCT TYPE IMAGES //
foreach ($productTypeImages as $img) {
$productTypeImagesNewName = hexdec(uniqid()) . '.' . $img->getClientOriginalExtension();
Image::make($img)->resize(917, 1000)->save('upload/products/product-images/' . $productTypeImagesNewName);
$productTypesImagesLink = 'upload/products/product-images/' . $productTypeImagesNewName;
}
// PRODUCT TYPE IMAGES ENDS //
foreach ($productTypes as $id => $key) {
ProductVariations::insert([
'product_id' => $productId,
'product_type' => $productTypes[$id],
'product_color' => $productColors[$id],
'product_weight' => $productWeight[$id],
//'product_variation_image' => $productTypesImagesLink[$id], <--- PROBLEM HERE
'product_variation_qty' => $productQtys[$id],
'product_variation_price' => $productPrices[$id],
'created_at' => Carbon::now()
]);
}
} else {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
'created_at' => Carbon::now()
]);
}
// PRODUCT MULTIPLE IMAGES //
$productImages = $request->file('product_image');
foreach ($productImages as $productImage) {
$productImagesNewName = hexdec(uniqid()).'.'.$productImage->getClientOriginalExtension();
Image::make($productImage)->resize(917,1000)->save('upload/products/product-images/'.$productImagesNewName);
$productImagesLink = 'upload/products/product-images/'.$productImagesNewName;
ProductImages::insert([
'product_id' => $productId,
'product_image' => $productImagesLink,
'created_at' => Carbon::now()
]);
}
// PRODUCT MULTIPLE IMAGES ENDS //
$notification = array(
'message' => 'Product Inserted!',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
I have come up with different solution.
After inserting product with multiple images and if product has any variations, I redirect user to another page. Where he can select image for each type if he wants to. And I'm inserting only image id from product_images table, to product_variations table field - product_variation_image.
By the way, I use a jQuery plugin called image-picker for that. And now everything works and my client is happy with that.
Product function:
public function product_store(Request $request)
{
// PRODUCT TYPE VARIABLES //
$productTypes = $request->product_type;
$productColors = $request->product_color;
$productWeight = $request->product_weight;
$productQtys = $request->product_qty;
$productPrices = $request->product_variation_price;
$productTypeImages = $request->file('product_variation_image');
// PRODUCT TYPE VARIABLES ENDS //
// PRODUCT COVER IMAGE //
$productCoverImage = $request->file('product_cover_image');
if($productCoverImage) {
$productCoverImageNewName = hexdec(uniqid()) . '.' . $productCoverImage->getClientOriginalExtension();
Image::make($productCoverImage)->resize(917, 1000)->save('upload/products/cover-images/' . $productCoverImageNewName);
$productCoverImageLink = 'upload/products/cover-images/' . $productCoverImageNewName;
} else {
$productCoverImageLink = 'upload/no-image/image.png';
}
// PRODUCT COVER IMAGE ENDS //
if(!empty(implode($productTypes))) {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
]);
foreach ($productTypes as $id => $key) {
ProductVariations::insert([
'product_id' => $productId,
'product_type' => $productTypes[$id],
'product_color' => $productColors[$id],
'product_weight' => $productWeight[$id],
'product_variation_qty' => $productQtys[$id],
'product_variation_price' => $productPrices[$id],
'created_at' => Carbon::now()
]);
}
} else {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
'created_at' => Carbon::now()
]);
}
// PRODUCT MULTIPLE IMAGES //
$productImages = $request->file('product_image');
if($productImages) {
foreach ($productImages as $productImage) {
$productImagesNewName = hexdec(uniqid()) . '.' . $productImage->getClientOriginalExtension();
Image::make($productImage)->resize(917, 1000)->save('upload/products/product-images/' . $productImagesNewName);
$productImagesLink = 'upload/products/product-images/' . $productImagesNewName;
ProductImages::insert([
'product_id' => $productId,
'product_image' => $productImagesLink,
'created_at' => Carbon::now()
]);
}
}
// PRODUCT MULTIPLE IMAGES ENDS //
$notification = array(
'message' => 'Product Inserted!',
'alert-type' => 'success'
);
if(!empty(implode($productTypes))) {
return redirect()->route('admin.product-variations-images-settings', ['id' => $productId])->with($notification);
} else {
return redirect()->route('admin.products')->with($notification);
}
}
Functions for image varations:
public function product_variations_images_settings($id)
{
$productVariations = ProductVariations::where('product_id', $id)->get();
$productImages = ProductImages::where('product_id', $id)->get();
return view('administrator.pages.products.product-variations', compact('productVariations','productImages'));
}
public function product_variations_images_store(Request $request)
{
$productVariationId = $request->id;
$productImageId = $request->product_variation_image;
$array = array_combine($productVariationId, $productImageId);
foreach ($array as $id => $key){
//dd($key);
ProductVariations::findOrFail($id)->update([
'product_variation_image' => $key,
'updated_at' => Carbon::now()
]);
}
$notification = array(
'message' => 'Images Set!',
'alert-type' => 'success'
);
return redirect()->route('admin.products')->with($notification);
}

cannot open file generated by PHPReport and PHPExcel

I have used the PHPReport library content with the PHPExcel Library content. I am creating the website locally on my macbook using Xampp. The following code I wrote to fetch the data and generate the excel sheet:
public function fetchReportData($newStartDate, $newEndDate){
if ($this->getDBConnection()) {
$q = "select * from PMA where request_date between '".$newStartDate."' and '".$newEndDate ."'";
$r = mysqli_query($this->dbc, $q);
if ($r) {
$row_count = $r->num_rows;
if ($row_count == 0) {
echo '<div align="left">'
. '<b><font color="red">* </font>No requests were found in this period</b></div>';
} else {
$data = array();
while ($row = mysqli_fetch_array($r)) {
$desc = $this->returnDesc($row[0]);
$requestId = $this->getNormalReqID($row[0]);
$this->get($requestId);
$username = $this->getUserName($this->requestor_id);
$srv_id = $this->getSRVId($row[0]);
$this->getSRV($srv_id);
$srv_date = date("d-m-Y", strtotime($this->srv_date));
$srv = 'SRV dtd '.$srv_date;
if($this->srv_remarks == null)
{
$remark = $srv;
}
else
{
$remark = $this->srv_remarks;
}
$data[] =
array("Date" => $row[2], "Originator" => $username, "Material No" => $this->material_number,
"Description" => $desc, "Quantity" => $row[5], "Order Number" => $this->pm_order_number,
"Request Number" => $row[7], "Gate Pass No"=> $row[10], "Gate Pass Date"=> $row[14],
"SRV/Date" => $srv, "Received Qty" => $row[11], "Remarks" => $remark)
;
}
include_once 'PHPReport.php';
$R=new PHPReport();
$R->load(array(
'id'=>'product',
'header'=>array(
'Date'=>'Date', 'Originator' => 'Originator', 'Material No' => 'Material No',
'Description' => 'Description' , 'Quantity' => 'Quantity', 'Order Number' => 'Order Number',
'Request Number' => 'Request Number', 'Gate Pass No' => 'Gate Pass No', 'Gate Pass Date'=> 'Gate Pass Date',
'SRV/Date' => 'SRV/Date' , 'Received Qty' => 'Received Qty', 'Remarks' => 'Remarks'
),
'footer'=>array(
'Date'=>'', 'Originator' => '', 'Material No' => '',
'Description' => '' , 'Quantity' => '', 'Order Number' => '',
'Request Number' => '', 'Gate Pass No' => '', 'Gate Pass Date'=> '',
'SRV/Date' => '' , 'Received Qty' => '', 'Remarks' => ''
),
'data'=> $data
)
);
echo $R->render('excel');
}
}
}
}
It does not show any error while calling the code and it generates the xlsx sheet fine but I cannot open the file. I get the following error when I try to open the sheet:
The format or file extension is not valid. verify that the file has not been corrupted and the file extension matches the format of the file.
I get the same error while trying to open a PDF created by the library PHP to PDF, is it something wrong with my macbook?

Categories