How to upload renamed image in codeigniter - image

I want to upload image in codeigniter. But how to upload renamed image, please see the below code.
$filename = $_FILES['filename']['name']."_".date("Y-m-d")."_".date("H:i:s");
public function index()
{
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
$this->form_validation->set_rules('author', 'Author', 'required');
$filename = $_FILES['filename']['name']."_".date("Y-m-d")."_".date("H:i:s");
$this->do_upload();
$this->load->model('News_model');
$this->News_model->insert_news($filename);
$this->load->view('news/success', $data);
}

this is what I'm doing to do that:
$folder = "./Path/To/Uploaded/files"
$config['upload_path'] = $folder;
// We initiate the CI upload library
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file')) {
$error = array('error' => $this->upload->display_errors());
} else {
// Upload success, we get back the image info
$data = array('upload_data' => $this->upload->data());
// we rename the file
$filename = $data['upload_data']['raw_name'] . "_".date("Y-m-d") . "_" . date("H:i:s") . $data['upload_data']['file_ext'];
rename($data['upload_data']['full_path'], $folder . $filename);
}
See details in Codeigniter documentation : https://www.codeigniter.com/user_guide/libraries/file_uploading.html

Related

How to upload multiple images in laravel so I can get them in frontend?

I want to do multiple image uploads in my laravel but so far i get errors like image must be image ... is chaos really ... help please???
public function store(Request $request) {
$request->validate([
'name' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
'image.*' => 'image',
'description' => 'required',
'quantity' => 'required',
]);
$imgData = [];
if($request->hasfile('image')) {
foreach($request->file('image') as $file)
{
$path = '/images/phones';
$date = Carbon::now();
$tempName = env('APP_URL') . '/storage' . $path . '/'. $date->year.'/' . time() . '.' . $file->getClientOriginalName();
$filename = time() . '.' . $file->getClientOriginalName();
$filePath = $path . "/$date->year";
$file->storeAs( $filePath, $filename, 'public');
$imgData[] = $tempName;
}
$phones = new Phone;
$phones->name = $request->name;
$phones->description = $request->description;
$phones->quantity = $request->quantity;
$phones->price = $request->price;
$phones->brand_id =$request->brand_id;
$phones->image = json_encode($imgData);
$phones->save();
return redirect()->route('phones.index')
->with('success','Product has been created successfully.');
}
So far I get this error that image must be image and another error is displaying required extensions!!

file uploading multiple times in codeigniter

i am trying to upload files in codeigniter.but my files are uploading multiple times.this is my controller code.
public function savedocument() {
if (isset($_FILES) && !empty($_FILES)) {
$category_id = $_POST['category'];
$category_name = $_POST['category_name'];
$config = array();
$config['upload_path'] = 'uploadpath';
$config['allowed_types'] = 'gif|jpg|png|pdf|doc|docx';
$config['max_size'] = '20480';
$config['overwrite'] = FALSE;
$this->load->library('upload', $config);
$files = $_FILES;
for($i=0; $i< count($_FILES['files']['name']); $i++)
{
$_FILES['files']['name']= $files['files']['name'][$i];
$_FILES['files']['type']= $files['files']['type'][$i];
$_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
$_FILES['files']['error']= $files['files']['error'][$i];
$_FILES['files']['size']= $files['files']['size'][$i];
$this->upload->initialize($config);
if (!$this->upload->do_upload('files')) {
$response['message'] = $this->upload->display_errors();
}
else {
$this->upload->do_upload('files');
$file_details = $this->upload->data();
//print_r($file_details);
$user_id = $this->session->userdata('user_id');
$uploaded_file_details = array(
'tmp_name' => $file_details["file_name"],
'file_name' => $file_details["orig_name"],
'size' => $file_details["file_size"],
'type' => $file_details["file_type"],
'category_name' => $category_name,
'category_id' => $category_id,
'upload_at' => date("Y-m-d H:i:s"),
'uploaded_by' => $user_id
);
$document_id = $this->document->CraeteNewDocument($uploaded_file_details);
}
redirect('admin/documents', 'refresh');
}
}
}
can anyone point out the issue.how to make if there is only one file to upload upload it once and if multiple files are selected.have to upload it.

How to upload image into database using Laravel

I am trying to upload an image into the database. The image is not uploading properly, how can I fix it?
The image path is uploading into database like this: {"image":"phpV3IZnF.png"}
it should be like this: phpV3IZnF.png
Controller
public function servicesaction(Request $request)
{
$this->validate($request,[
'name' => 'required',
'description'=>'required',
'image' =>'required']);
$name=$request->get('name');
$description = $request->get('description');
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
Storage::disk('cms')->put($image->getFilename().'.'.$extension,
File::get($image));
$content = new Services;
$content->image = $image->getFilename().'.'.$extension;;
check = Services::where('id', $content->id)->select('image')-
>create(['service_name'=>$name,'description'=>
$description,'image'=>$content ])->get();
return back()->with('success', 'Success Successfully Inserted')-
>with('path', $check);
}
you can get file name like below
$fileName = md5($image->getClientOriginalName() . time()) . "." . $image->getClientOriginalExtension();
then save the name into database
Replace servicesaction function with below code
public function servicesaction(Request $request)
{
$this->validate($request,[
'name' => 'required',
'description'=>'required',
'image' =>'required']);
$name=$request->get('name');
$description = $request->get('description');
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
Storage::disk('cms')->put($image->getFilename().'.'.$extension,
File::get($image));
$content = new Services;
$content->image =$image->getClientOriginalName().'.'.$extension;;
check = Services::where('id', $content->id)->select('image')-
>create(['service_name'=>$name,'description'=>
$description,'image'=>$content ])->get();
return back()->with('success', 'Success Successfully Inserted')-
>with('path', $check);
}
change getFilename() to getClientOriginalName()
Like this
$extension = $image->getClientOriginalExtension()
$content->image = $image->getClientOriginalName().'.'.$extension;
Just Change 'image'=>$content to 'image'=>$content->image in the your create function
check = Services::where('id', $content->id)->select('image')
->create(['service_name'=>$name,'description'=> $description, 'image'=>$content->image ]) //use $content->image here
->get();

Laravel user post image upload error

I have error while i am uploading Post Image I can save picture on my computer but in my database filename is distorted.
Here is my PostsController
public function store(Request $request, User $user, Image $image)
{
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'body' => 'required'
]);
if( $request->hasFile('image') ) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->save( public_path('uploads/images/' . $filename ) );
}
$image = $filename;
auth()->user()->publish(
new Post(request(['body', 'image'])));
return redirect('/');
}
The problem is that you try to insert the wrong variable into your database table. So, improve your code like this:
auth()->user()->publish(
new Post(['photo' => request('body'), 'image' => $image])
);

How to get the uploaded filename from upload.php page Codeigniter

I have Upload.php page as follows=>
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pgn';
$config['max_size'] = 0;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$config['detect_mime'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
and the Hesaplama.php controller as =>
$upload_data = $this->upload->data(); //(line 51) Should return array of containing all of the data related to the file you uploaded.
$file_name = $upload_data['file_name'];
$file = fopen("<?php echo site_url('uploads/$file_name'); ?>", "r");
while(! feof($file))
{...
However I get the following error =>
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Hesaplama::$upload
Filename: controllers/Hesaplama.php
Line Number: 51
Backtrace:
File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 51
Function: _error_handler
File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 249
Function: pgn_oku
File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 28
Function: pozisyon_tutma
File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
How can we remedy that and get the file name of the newly uploaded file?
I personally think that the program does not recognize the data in another page(controller).
Thank you...
Sorry i can't comment yet, so i write it here. I think the library for
$this->upload->data();
wasn't loaded.
if you want to get only the filename, maybe you can try using session.
in Upload controller:
$data = $this->upload->data();
$this->session->set_userdata('filename', $data['file_name']);
in Hesaplama :
$filename = 'uploads/'.$this->session->userdata('filename');
$file = fopen("<?php echo site_url('$filename'); ?>", "r");
hope this help.
to get the filename and extention file to save it in a db you can use tis
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pgn';
$config['max_size'] = 0;
$config['detect_mime'] = TRUE;
$this->load->library('upload', $config);
if( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
$upload_data = $this->upload->data();
/*
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
*/
$this->model->save_img_info($upload_data['file_name']);
$data = array('upload_data' => $upload_data );
$this->load->view('upload_success', $data);
}
}
}
?>

Resources