Uploading file image Using CI - php

in my view, I used form_open_multipart(), and I have:
<input type="file" name="user_photo" style="margin-top: 5px;" />
I have a configuration in my controller like this:
$config['upload_path'] = './resources/uploads/';
$config['allowed_types'] = 'jpg|gif|png|bmp|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('user_photo')) {
echo $this->upload->display_errors('<p>', '</p>');
} else {
echo "<pre>"; print_r($this->upload->data());
}
But, when I try to upload image file with filetype *.jpg, I've an error like this: The file type you are attempting to upload is not allowed.Is there any wrong with my configuration,.?
thanks,.

If your web server is using PHP v5.2.x and you're using CodeIgniter 2.1.0, downgrade back to CodeIgniter 2.0.3 and the upload problem should go away.
The guilty line of code that creates this issue in CodeIgniter 2.1.0 is
$this->file_type = #mime_content_type($file['tmp_name']);
in /system/libraries/Upload.php. See the list of CodeIgniter open issues for more information.

Related

Codeigniter Upload function does not work

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;
}

Store image path in codeigniter

Hello I have a write a code that upload image path into the database but the thing is it uploads the real path (C:/wamp/www/amref/images/student_profile/childbrah2.jpg) hence when I fetch the path form the database in order to display the image it gives me the error that the link is broken. I want to store only (images/student_profile/childbrah2.jpg).
The controller code:
$config['upload_path'] = '../images/student_profile/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = "2048000"; // Can be set to particular file size , here it is 2 MB(2048 Kb)
$config['max_height'] = "768";
$config['max_width'] ="1024";
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
echo '<h4 style="color: red;">
Failure! </h4>
<p style="color: red;">'.$this->upload->display_errors().'</p>';
} else {
$data_upload_files = $this->upload->data();
$image = base_url($data_upload_files['full_path']);
Yes. Because you inserting $config['upload_path'] = '../images/student_profile/', so path is C:/wamp/www/amref/images/student_profile/.
If you enter this as C:/wamp/www/amref/images/student_profile/ this will not work in your server. Because in your server there is no partition call C:.
So use your path as it is.
Edit
<?php
$old = 'C:/wamp/www/amref';
$fulpath = $old.'/images/student_profile/';
echo $fulpath;

PHP File Uploading working in local but not working in server

As I'm uploading a csv file in localhost it is correctly working. All the validations are done but while uploading the file online it is not working. The file is refreshing itself.This is my sample code
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '10000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$data['mainpage']='category';
$data['mode']='addcsv';
$this->load->view('includes/mainpage',$data);
}
else
{
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
Please check your write permission to the directory you want to upload.
Are you getting any error messages thrown while uploading the file?
Also make sure that you have set the correct permissions to the upload directory you are trying to upload the file to.
It would also be nice with some code.
Use move_uploaded_file() function like below
move_uploaded_file($_SERVER['DOCUMENT_ROOT'].'/'.$_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
What's the output of $this->upload->display_errors(); ?
You can also check the folder permissions on the server (0777)

Trying to upload photo in Codeigniter - not finding my upload folder - ideas why?

I'm trying to upload profile pics during a registration process with CI ($me !== 'experienced' )
Here is my method ... nothing strange
function do_upload()
{
$config['upload_path'] = base_url('/upload_pic/');
$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() )
{
$data['message'] = $this->upload->display_errors() . $config['upload_path'];
$this->load->view('profile_photo_view', $data);
}
else
{
$data['message'] = $this->upload->data();
$this->load->view('upload_success', $data);
}
}
.. but it is failing. As you can see, I have the upload path passing to my fail page for display, and this is what it is showing..
The upload path does not appear to be valid.
http://localhost:8888/MY_SITE/upload_pic
That is where my upload folder is. And I have full 777 permissions on it..
.. any idea what could be wrong? And will trailing slashes always be stripped like that?
base_url will give you...wait for it...a URL :)
You need a path for uploads.
$config['upload_path'] = '/full/path/to/upload_pic/';
or you can use relative paths:
$config['upload_path'] = '../upload_pic';
but you have to give it a path, not a URL.
As #stormdrain pointed out, base_url() is there to get the URL, not a filesystem path. Try:
$config['upload_path'] = dirname(FCPATH) . '/upload_pic/';
Try this
$config['upload_path'] = base_url() . '/upload_pic/';
or inside index.php append this
define('UPLOADS', 'upload_pic');
then set config to
$config['upload_path'] = UPLOADS;

Codeigniter File uplaod

Hi I am trying to use the codigniter do_upload function below:
But get this error: "You did not select a file to upload." I do select a file and when i pass the name i can see the name but still can not upload...thanks in advance for the help.
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';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$data = array('error' => $this->upload->display_errors());
print_r($data);
exit;
//$this->load->view('clients_page', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($data);
exit;
}
}
I assume you're not naming your <input type="file" /> the default userfile. It would be nice to see your upload view also.
change this
$this->upload->do_upload()
to this
$this->upload->do_upload('audioRef')
I do select a file and when i pass the name i can see the name but still can not upload
This is often because you've omitted to set your form to submit as multipart/form-data. Forms default to application/x-www-form-urlencoded, which can't contain file uploads; in this case you just get the filename instead.
<form method="post" action="..." enctype="multipart/form-data">
Incidentally,
$config['allowed_types'] = 'gif|jpg|png';
if you must use filename extension to check file type (and it's inadvisable, but that's what CodeIgniter offers, unfortunately), please also allow jpeg and any other likely extensions.
$config['upload_path'] = './uploads/';
If that uploads folder is within the webroot (you'll be serving files directly out of it), that's potentially a cross-site-scripting vulnerability. Unfortunately it is possible to upload files that look like images but in fact contain HTML, including <script>.
Doing user-uploaded-content in a safe way is hard. See eg this question for background.

Categories