file_get_contents() expects parameter 1 to be a valid path
10,814
$data contain array values, you have to use upload_data
for ex :
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data )
);
change this to
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data['upload_data'] )
);
or
you can directly use upload data function
$data = array(
'image' => file_get_contents( $this->upload->data())
);
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
My image is uploading to images/news successfully. I want to now save that image in the database. but it keeps giving me the above error. My model query is correct because i use it to insert other data and it works.
How can i save the image into the database once the user clicks submit?
My controller:
function news() { $config = array( 'upload_path' => "./images/news", 'allowed_types' => "gif|jpg|png|jpeg|JPG", 'overwrite' =>False, 'max_size' => "2048000", 'max_height' => "768", 'max_width' => "1024" ); $this->load->library('upload', $config); $this->load->helper(array('form', 'url')); if($this->upload->do_upload()) { $data = array('upload_data' => $this->upload->data()); $this->load->view('manage_news',$data); } else { $message2 = "Please choose another file type. gif,jpg,png,jpeg,pdf "; echo "<script type='text/javascript'>alert('$message2'); </script>"; redirect('resetPasswordController/add_news', 'refresh'); } $this->db->trans_start(); $data = array('upload_data' => $this->upload->data()); $data = array( 'image' => file_get_contents( $data ) ); $this->load->model('users_model'); $this->users_model->insert($data); $this->db->trans_complete(); $this->db->trans_complete(); redirect('resetPasswordController/manage_news', 'refresh'); }
my view:
<?php echo form_open_multipart('resetPasswordController/news');?> <label>Image</label> <input name = "userfile" type="file" />
my model:
function insert($data){ $this->db->insert('news',$data); return $this->db->insert_id(); }
-
Yes Kay Selva almost 9 yearstry direct file path file_get_contents( /* upload file paht */ );
-
Admin almost 9 yearsthis is what i did: $data = array('image' => file_get_contents( "./images/news" ) );
-
Yes Kay Selva almost 9 yearswhere is your file name put your full file path
-
Admin almost 9 yearsthis worked -> $imagename = $this->upload->data(); $data = array('upload_data' => $this->upload->data());