I have a problem uploading image in a Codeigniter app I'm working on. The code was working when on my pc (ubuntu) just fine, as it's given in tutorials. But when I'm trying this in my (centos) server, it's giving an error
"You did not select a file to upload.".
Just for your reference, I'll paste the code here.
Controller- Upload1.php
<?php
class Upload1 extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form1', array('error' => ' ' ));
}
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())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form1', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success1', $data);
}
}
}
?>
Form - upload_form1.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload1/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Success Page- upload_success1.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload1', 'Upload Another File!'); ?></p>
</body>
</html>
What could be the problem?
I have "uploads" directory set to 777. So that's not likely to be the reason. I also tried couple of solutions to similar problems, but isn't working. Have missed something obvious?
Versions:
For Server- PHP 5.3.13 (cli)
My PC- PHP 5.3.3 .
Codeigniter is same on local and remote machines- that is 2.1.3
I will be grateful for your suggestions!!
Thank you so much
based on your code, i don't see if there is any form validation to validate the submitted input whether it's valid with your upload config or not. What you should do first : Check the file that you're trying to upload is appropriate with the upload config or not. I think CI's upload class doesn't cover errors from a non valid input form. CMIIW.
normally this is the error when you are not taking the name as "userfile" so check the output by 'view source' in any browser
Related
I have a detailed codeigniter project code is below:
config.php file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['base_url'] = 'http://localhost/cicrud/codepolitan/Blog';
With the folder path controllers/codepolitan/Blog.php
<?php
class Blog extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('codepolitan/Blog_model');
$this->load->database();
$this->load->helper(array('form','url'));
}
//Tambah
public function add()
{
if ($this->input->post()) {
$data['title'] = $this->input->post('judul');
$data['content'] = $this->input->post('konten');
$data['url'] = $this->input->post('link');
$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('cover'))
{
echo $this->upload->display_errors();
}
else
{
print_r($this->upload->data());
exit;
}
print_r($data);
// var_dump($data);
$this->Blog_model->adddata($data);
}
$this->load->view('/codepolitan/form_add');
}
}
Display files from the form.
With the folder path view/codepolitan/form_add.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php echo form_open_multipart();?>
<div>
<label>Judul</label>
<input type="text" name="judul">
</div>
<div>
<label>URL</label>
<input type="text" name="link">
</div>
<div>
<label>Konten</label>
<textarea name="konten" cols="30" rows="10"></textarea>
</div>
<div>
<label>Upload Cover</label>
<?php echo form_upload('content', null, 'class="form-control"')?>;
</div>
<button type="submit">Simpan Artikel</button>
</form>
</body>
</html>
When running, choose a jpg or png file format and add the code in url to be duplicated like this http://localhost/cicrud/codepolitan/Blog/codpolitan/Blog/add
If, add function code "add()" from controller "Blog.php" at There is an error message "You did not select a file to upload." Whereas previously I had chosen a jpg or png file format.
Please provide an explanation why it is like that? How is the solution from the code above done?
When I try to upload a image to my uploads folder i get the following error:
Array ( [error] =>
You did not select a file to upload.)
My View:
<form action="<?=base_url('create_public_profile_job') ?>" class="create-profile-form" method="POST">
<input type="file" name="userfile" size="20000" />
<button type="submit" class="create-profile-button">Submit</button>
</form>
My Controller
public function create_public_profile_job()
{
if(isset($_POST['userfile']))
{
$this->User_model->do_upload($_POST['userfile']);
}
}
My Model
public function do_upload($userfile)
{
$this->load->helper('form');
$this->load->helper('url');
$config['upload_path'] = 'assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000000;
$config['max_width'] = 10240000;
$config['max_height'] = 7680000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($userfile))
{
$error = array('error' => $this->upload->display_errors());
print_r($userfile);
print_r($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($data);
}
}
The Model part is from the Codeigniter userguide https://codeigniter.com/userguide3/libraries/file_uploading.html
Don't really know where the issue is, because I pass the image through all functions
change view to
<form action="<?=base_url('create_public_profile_job') ?>" class="create-
profile-form" method="POST" enctype="multipart/form-data">
<input type="file" name="userfile" size="20000" />
<button type="submit" class="create-profile-button">Submit</button>
</form>
You have to add the enctype="multipart/form-data" in the form tag to upload the data through the forms.
So I figured out what my problem was. Or better problems.
My first Problem was that I had to add to my form tag: enctype="multipart/form-data" as others already pointed out in the comments
My second problem was that I had to add in my autoload.php file $autoload['libraries'] = array('database',.., 'upload');
Last but not least I changed $this->load->library('upload', $config); to $this->upload->initialize($config);
Somehow the configuration was not able to initialize correctly, so this fixed my last problem.
I'm having some trouble with CodeIgniter 2.1.4 and uploading files via AJAX.
I currently have a form which submits perfectly, like so (views/load_form):
<!--form created to add member to database -->
<?php echo form_open_multipart('index.php/member_record/add_member'); ?>
<?php echo form_fieldset('Create Membership Record'); ?>
<div class="container">
<form id="memberform" role="form">
<?php echo form_label('First Name', 'fname'); ?>
<?php echo form_input('fname'); ?>
</div>
<!--other such fields omitted for brevity-->
<label>Upload scanned ID Card</label>
<input type="file" name="scIDFile" id="scIDFile" />
<p class="help-block">Select image as either JPEG, GIF, PNG</p>
<input id="submit" type="submit" name="submit" value="Create Record" class="btn btn-success"/>
<!--end of view-->
I have a Controller which handled this well (controllers/member_record):
public member_record extends CI_Controller{
public add_member(){
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size']= 1024*8;
$config['encrypt_name']=TRUE;
$config['overwrite'] =TRUE;
$this->load->library('upload',$config);
foreach($_FILES as $field => $file){
if($file['error'] == 0){
if($this->upload->do_upload($field)){
/*$image_config=array(
'source_image'=>$data['full_path'],
'new_image'=>$data['file_path'].'/thumbs',
'maintain_ratio'=>true,
'width'=>150,
'height'=>100
);
$this->load->library('image_lib',$image_config);
$this->image_lib->resize();*/
}else{
$errors = $this->upload->display_errors();
}
}
}
}
}
And this was fine. However, due to certain new functionality that I'm adding to my view: I need to convert things to AJAX and pass all these values to the back-end through AJAX. I've been able to submit all of the fields (like first-name, last-name) to the backend via POST, however I'm struggling to enable file uploads!
I've tried using jQuery File Upload plugins, but there aren't very clear directions on how one would use them for CodeIgniter 2 (especially given my current code). If anyone can shed some light on this, I would appreciate it.
I have a form with text inputs and a file input. What is a proper way validate both input types using Codeigniter's validation library? I found some solutions but they don't work properly or seem like an overkill (creating new libraries or modifying CI system files).
In my view I'm using 1 multipart form and displaying both text validation error and upload errors.
Here is what I have so far in my Controller...
function create() //create new post
{
$this->form_validation->set_rules('content', 'Entry', 'trim|required|xss_clean');
$this->form_validation->set_rules('category_id', 'Category', 'trim|required|xss_clean|integer');
//Text input fields
if ($this->form_validation->run() == FALSE)
{
$this->load->view('new_post');
}
else
{
$config['upload_path'] = './uploads/posts/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '800'; //in KB
$this->load->library('upload', $config);
//File Upload
if (! $this->upload->do_upload())
{
$upload_error['upload_error'] = array('error' => $this->upload->display_errors());
$this->load->view('my_view', $upload_error);
return FALSE;
}
//Add to database
$data = array (
'user_id' => $this->tank_auth->get_user_id(),
'category_id' => $this->input->post('category_id'),
'content' => $this->input->post('content')
);
$this->Posts_model->create_post($data);
$this->session->set_flashdata('success', 'Post_added!');
redirect('posts');
}
}
I keep getting You did not select a file to upload. in my view.
What is the name of your file input? do_upload() is expecting it by default to be 'userfile', however if you have something like <input type="file" name="image"... you will need to call $this->upload->do_upload('image')
You also need to make sure the form is set to multipart - http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
You're using CodeIgniter's helpers correctly, this could be running more into a PHP problem then a CodeIgniter problem.
It looks like your file may be too big for your PHP configuration? PHP doesn't seem to be passing the file along to you. Create a php_info() file and see what UPLOAD_MAX_FILESIZE is set to?
Also, make sure you have the extension and mime type pair set in application/config/mimes.php.
default codeigneter upload class is $this->upload->do_upload('field_name = userfile') , so you can set the name of field file you make or you use default codeigneter name field of file , if you want to change the name field for file type in your form , please check how codeigneter made setting for that class, actually is simple class and easy to use.., because that class need parameter name of file
Form field validation with file upload using codeigniter
Controller: Upload.php
Note: create folder uploads at base level
Upload folder store file..
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
//$this->load->view('upload_form', array('error' => ' ' ));
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
//Text input fields
if ($this->form_validation->run() == FALSE)
{
$this->load->view('upload_form');
}
else
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$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);
}
}
}
}
?>
Form:(upload.form.php)
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open_multipart();?>
Title: <input type="text" name="title" size="30" /><br/>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
**upload_success.php**
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open_multipart();?>
Title: <input type="text" name="title" size="30" /><br/>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Im trying to make a file upload in CodeIgniter, how ever when I add enctype="multipart form-data" no post data will go through. At all not even the other fields. However when i dont add it, i can get the other post data, but of course no file upload. Whats going wrong here. Here is my view and controller:
View:
<h2>Add a New Album</h2>
<form enctype="multipart/form-data" method="post" action="<?php echo base_url();?>index.php/photo/newAlbum">
<table style="margin-left:5px;">
<tr>
<td> Album Name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td> Photo .zip File:</td>
<td><input type="file" name="userfile" size="20" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Upload Photo File" /></td>
</tr>
</table>
</form>
controller only contains:
var_dump($_POST);
Result is:
array(0) { }
You can add this form attribute:
enctype="multipart/form-data;charset=utf-8"
Instead of:
enctype="multipart/form-data"
You can see this link.
Actually, the multipart data like image/zip or some other blob data will be included in $_FILES array, not $_POST array.
I recommend you to use the upload library.
view:upload_form.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
view:upload_success.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
controller:upload.php
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
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())
{
$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 that's all
You need fix your do_upload, like this..
$this->upload->do_upload('name-of-input-file-element-of-your-form')
For example, in your view code, you have:
<input type="file" name="userfile" size="20" />
So, your do_upload line, should be like this:
$this->upload->do_upload('userfile')
Saludos!
Interesting. The code you posted should work. I made a quick test script using your form, and both the $_FILES and $_POST data come through fine.
I found this previous question on SO: PHP--parsing multipart form data
It sounds like the same problem you're having. Unfortunately no real answer was reached for that problem. I would think this might be a server configuration issue - can you try the same code on another server and see if it functions?
The only other bit of information I can find was in the answer to this question, $_POST data returns empty when headers are > POST_MAX_SIZE
If you are trying to upload too large a file, apparently that can cause PHP to throw away the $_POST data as well. But if you've tried submitting your form without uploading a file (but still using "multipart/form-data"), and still don't see any $_POST data coming through, that doesn't apply.
I just had the same problem.
I solved it this way. in application/config/config.php you set the base_url
please be aware of, that this url have to be exact the same as the one you're calling
your web app. For me, I called my domain with www.sdfsd.com, but in the config file
I wrote http://sdfsd.com and so the POST of the form didn't work. So I changed this
and now everything is working!!
cheers
Check for possible wrong redirection in CI layer. It could be caused by a wrong URL request (for example, a slash '/' missing at the end of the URL). Redirections delete any POST data (I was dealing with this).
So, be sure that you are using full URLs in form action.
Check for your .htaccess and $config['uri_protocol'] consistency too.
I had this problem too. I don't agree with the above answers. I think the crucial thing is at what stage in your controller you attempt to get the post data. Perhaps you could post the relevant part of your controller. I found that both the post data and the upload data only become available after you call
this->upload->do_upload()
Obviously, you use an if statement to check for success. If successful,
$this->upload->data()
returns the standard array with info about the uploaded file and
$this->input->post('name_of_your_form_element')
is populated with what you entered in the relevant form element.
I had a similar problem - Turns out POST data is empty when you exceed max upload size / max post size.
This answer fixed my problem and allowed me to see $_POST and $_FORM data:
$_POST data returns empty when headers are > POST_MAX_SIZE