whenever i try to upload a file by pressing 'upload file' after choosing, it directs me to a blank page.tried the official documentation as well as various videos on youtube.
public function index()
{
$this->load->view('upload_form');
}
//upload_form
<?php
echo form_open_multipart(base_url()."index.php/home/upload_file");
echo form_upload("file");
echo form_submit("upload","Upload file");
?>
public function upload_file()
{
if($this->input->post("upload")===false)
return;
$config['upload_path'] = "./uploads/";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload("file"))
{
$this->load->view("error");
}
else
{
$this->load->view("success");
}
}
error and success pages are just simple texts.
($this->input->post("upload")===false)
will not work. Use this instead:
if(!empty($_FILES['upload']['name'])) {
...
}
Related
I am working on codeigniter and during creation of one of the APIs I got the issue. I tried to upload the image on Server as a file, while searching on the web, I got familiar with inbuild upload class in codeigniter. Please have a look at this code. I am sending file from Android using this tutorial.
public function upload_image_post(){
$config['upload_path'] =base_url().'/uploads/';
$config['file_name'] = rand() .'.jpg';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 10000;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
// $file = $this->input->post('file');
$this->load->library('upload', $config);
// $this->upload->initialize($config);
$file=$_FILES['uploaded_file'];
// $this->upload->do_upload($file);
if($file){
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'asQ',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is working*/
$res = $this->db->insert('ww_portfolio_images',$content);
}else{
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'not file',
'sp_id'=>'asQaaaaa',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is not working, Thats Obvious*/
$res = $this->db->insert('ww_portfolio_images',$content);
}
// $destinationPath=APPPATH.'public/assets/uploads/ANKO.jpg';
if($this->upload->do_upload('uploaded_file')) {
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'aaaaaaaaaaaaaaaaas',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is not working*/
$res = $this->db->insert('ww_portfolio_images',$content);
$this->response(['result' =>'Success',] , REST_Controller::HTTP_OK);
// return ($arr_image_info['full_path']);
}
else{
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'asass',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
$res = $this->db->insert('ww_portfolio_images',$content);
/* This is working*/
$this->response(['result' => 'ERrro'] , REST_Controller::HTTP_OK);
// $this->response(['result' =>'Image error',], 433);
}
}
I can not figure out the problem I am facing here. I am receiving a file but it does not upload.
I have also tried to use $this->upload->do_upload() instead of $this->upload->do_upload('uploaded_file') and this $config['max_size'] = '10000'; instead of this $config['max_size'] = 10000; . Please help. Any help would be greatly appreciated.
Also, when this code run through web panel, it working fine.
Better if you provide some more detail for the type of error or warning that you are observing.
There could be number of reasons.
1) base_url() gives you the public URL. You have to specify the absolute or relative path to your upload folder.
2) (if you are using an Apache Server) Your Apache user don't have the write permission to the upload folder.
3) Folder path doesn't exists.
If the first one doesn't work, please check the rest of the points. Hope this help you.
Regards
Muaaz
Try using FCPATH
$config['upload_path'] = FCPATH . '/uploads/';
Or
$config['upload_path'] = './uploads/';
Then http://www.codeigniter.com/user_guide/libraries/file_uploading.html#the-controller
<?php
class Example extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('form');
}
// Name function what every you want remember to change it on view form.
public function upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('uploaded_file')) {
// Then the success stuff
$upload_data = $this->upload->data();
echo $upload_data['file_name'];
} else {
// Errors
}
}
}
On view I would use the form helper functions form_open_multipart()
<?php echo form_open_multipart('example/upload');?>
<?php echo form_upload('uploaded_file', 'Upload');?>
<?php echo form_close();?>
check your form input in the view
My form view :
<input id="document" type="file" data-browse-label="browse" name="document" data-show-upload="false" data-show-preview="false" class="form-control file" />
other can be
permission issue for the uploads folder
if the folder does not exist you have to create it
And always try to debug the code with logs
document in the do_upload is the name of the input element in my view
if ($_FILES['document']['size'] > 0) {
$this->load->library('upload');
$config['upload_path'] = 'uploads/images';
$config['allowed_types'] = '*';
$config['max_size'] = $this->allowed_file_size;
$config['overwrite'] = false;
$config['encrypt_name'] = true;
$this->upload->initialize($config);
if (!$this->upload->do_upload('document')) {
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect($_SERVER["HTTP_REFERER"]);
}
$photo = $this->upload->file_name;
}
I'm trying to post a file to my CodeIgniter backend in the simplest way possible.
I want to fire an API call from Postman like this:
And my PHP function is looking like this right now:
public function runk_image_put()
{
$image = $this->upload->data();
$id = $this->get('id');
$this->response($image, REST_Controller::HTTP_OK);
}
I'm pretty sure that:
$image = $this->upload->data();
Is not the correct way of grabbing the posted file. What is the correct way?
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
if ( ! is_dir($config['upload_path']) ) die("THE UPLOAD DIRECTORY DOES NOT EXIST");
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('data')) {
echo 'error';
} else {
return array('upload_data' => $this->upload->data());
}
}
and in your controller use this:
$this->data['data'] = $this->do_upload();
and make the form as form-data or if you use binary send a header of what the file is like jpeg or what and then echo the file
I had seen many videos and took reference from CI user guide but became unable to find out error.When file is submitted from the form,it sends program flow the the uploadImage() method below.Please help me with the way.
Thank you
My code:
public function uploadImage()
{
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
if(!$this->upload->do_upload())
{
$this->load->view('upload');
}
else
{
$this->upload->display_errors();
}
}
I don't know what are you trying to do as plz see below may help..
public function uploadImage()
{
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
//load upload class library
$this->load->library('upload', $config);
//$this->upload->do_upload('filename') will upload selected file to destiny folder
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('edit', $upload_error);
}
else
{
// case - success
//callback returns an array of data related to the uploaded file like the file name, path, size etc
$upload_data = $this->upload->data();
$data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' .$upload_data['file_name']. '</strong> was successfully uploaded!</div>';
//$this->load->view('edit_profile', $data);
redirect(base_url("Display_somepage/index"));
}
//this below may be helpful to for debugging
echo $this->image_lib->display_errors();
I think it is a syntax error, notice the arrow.
public function uploadImage()
{
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
if($this->upload->do_upload('YOUR_FILE_INPUT_NAME')) <=========== HERE
{
$this->load->view('upload');
}
else
{
$this->upload->display_errors();
}
}
Basically, you are telling it to load the view when the uploading succeed, else show errors if the operation fails not the opposite.
How do I upload an image when I'm trying to save other data along with it? When the form submits, it hits the save function:
function save() {
$this->save_data($_POST, $_FILES);
}
function save_data($post_data, $file_data) {
// if theres an image
if(!empty($file_data['image']['size'])) {
$path = '/images';
$this->upload_image($path);
}
}
function upload_image($path) {
// CI is looking for $_FILES super global but I want to pass that data in
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->data('image');
$this->upload->do_upload('image');
}
I can't figure out how to actually pass the file data to another function. All the examples I've seen shows the form submitting to a function that uploads the function right from it. I want to do the uploading from another function though.
if you are trying to check if file is actually beeing uploaded do following
//this is optional
if (empty($_FILES['userfile']['name'])) {
$this->form_validation->set_rules('userfile', 'picture', 'required');
}
if ($this->form_validation->run()) { //if using validation
//validated
if (!empty($_FILES['userfile']['name'])) {
//picture is beeing uploaded
$config['upload_path'] = './files/pcitures';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
//$error = array('error' => $this->upload->display_errors());
} else {
//no error, insert/update in DB
$tmp = $this->upload->data();
echo "<pre>";
var_dump($tmp);
echo "</pre>";
}
} else { ... }
}
My mistake was related to folder permissions.
For those of you that are looking to split the upload functionality into multiple functions:
Controller:
$save_data = $this->save_model->save_data($_POST, $_FILES);
Model:
function save_data($data, $file) {
// check if theres an image
if (!empty($file['image']['size'])) {
// where are you storing it
$path = './images';
// what are you naming it
$new_name = 'name_' . random_string('alnum', 16);
// start upload
$result = $this->upload_image($path, $new_name);
}
}
function upload_image($path, $new_name) {
// define parameters
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
// upload the image
if ($this->upload->do_upload('image')) {
// success
// pass back $this->upload->data() for info
} else {
// failed
// pass back $this->upload->display_errors() for info
}
}
I looked for this in Google and different answers on stackoverflow. And probaly there is an good answer in them but still i don't get how i can implent it in my own code.
I got my own public function to upload an image, but now i want it to be optional. At this moment someone needs to upload an file to pass the validation, how can i make this optional?
my function:
public function _do_upload_image()
{
$config['upload_path'] = './company_images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$this->form_validation->set_message('_do_upload_image', $this->upload->display_errors());
}
else
{
$this->_upload_data = $this->upload->data();
}
}
Thanks in advance
--edit--
For other people, the answer worked and i gave my file_name an other name when there was no image uploaded. It looks like this:
public function _do_upload_image()
{
$config['upload_path'] = './company_images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$returnArr = array();
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$returnArr = array('file_name' => 'leeg.jpg');
}
else
{
$returnArr = $this->upload->data();
}
$this->_upload_data = $returnArr;
}
If you mean you need to make your upload function optional then you can just do:
public function _do_upload_image()
{
$config['upload_path'] = './company_images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$returnArr = array();
$this->load->library('upload', $config);
if ($this->upload->do_upload())
{
$returnArr = $this->upload->data();
}
return $returnArr; //just return the array, empty if no upload, file data if uploaded
}
Hope that makes sense
codeigniter file upload optionally ...works perfect..... :)
---------- controller ---------
function file()
{
$this->load->view('includes/template', $data);
}
function valid_file()
{
$this->form_validation->set_rules('userfile', 'File', 'trim|xss_clean');
if ($this->form_validation->run()==FALSE)
{
$this->file();
}
else
{
$config['upload_path'] = './documents/';
$config['allowed_types'] = 'gif|jpg|png|docx|doc|txt|rtf';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile',FALSE))
{
$this->form_validation->set_message('checkdoc', $data['error'] = $this->upload->display_errors());
if($_FILES['userfile']['error'] != 4)
{
return false;
}
}
else
{
return true;
}
}
i just use this lines which makes it optionally,
if($_FILES['userfile']['error'] != 4)
{
return false;
}
$_FILES['userfile']['error'] != 4 is for file required to upload.
you can u make it unneccessory by using $_FILES['userfile']['error'] != 4 , then it will pass this error for file required and
works great with other types of errors if any by using return false ,
hope it works for u ....