I am trying to upload a file using codeigniter and no matter what I have tried I am getting back the error message that I have not selected a file when I actually have.
My controller looks like so
public function editHeader()
{
$this->require_auth();
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000KB';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
echo('yeah!');
}
}
and my html is just a codeigniter form
<?foreach($header as $row) :?>
<?=form_open_multipart('admin/editHeader');?>
<input class="waitForLoad fadeInDown" type="file" name="userfile" />
<input type="submit" name="submit" value="Change" class="btn btn-success pull-right" />
</form>
<?endforeach;?>
I have tried the normal solutions such as upping the max_size as well as putting the upload name in the do_upload function, but nothing seems to work and I am about to pull my hair out. Any help is appreciated!
You need to use form_open_multipart() instead of form_open() when uploading files.
You must use
<?php echo form_open_multipart(path); ?> because we get the $_FILES in controller if we write in the form.
After adding it, if u print_r($_FILES) then you will get the array.
It is necessary when we do the work related to image upload or captcha
Related
Trying to read the file name of a file that is uploaded as part of a form. The print_r command that I'm using to test just shows a blank screen. I have read the manual (near the bottom here) pertaining to this and don't understand what I'm doing wrong.
Controller:
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt|pdf|xlsx|csv|xls|bmp';
$config['max_size'] = 1000;
$this->load->library('upload', $config);
$file_name = $this->upload->data('file_name');
print_r($file_name);
View:
<?php echo form_open_multipart('Corpmuns/do_upload', array('method' => 'post'));?>
... // some drop-down menus and text fields here
<INPUT TYPE="file" NAME="userfile" id="userfile" >
</form>
You didn't do the upload action so the file was not uploaded yet. That's why you can't get the uploaded file's name. Because it does not exists.
Code $this->upload->do_upload('userfile') and make sure the file is uploaded successfully before you get the filename.
Call do_upload function from upload library.
Update Controller:
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt|pdf|xlsx|csv|xls|bmp';
$config['max_size'] = 1000;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile')) { //use this function
$data['error'] = false;
$upload_data = $this->upload->data();
$data['data'] = $upload_data;
$data['msg'] = 'Image Successfully Uploaded.';
} else {
$data['msg'] = $this->upload->display_errors('', '<br>');
}
print_r($data)
}
if ( ! $this->upload->do_upload('input_name'))
{
echo $this->upload->display_errors();
}
else
{
$file=$this->upload->data();
echo $image=$file['file_name'];//Set file name to varilable
}
}
<form action="" enctype="multipart/form-data" method="post"
name="uploadfile">
you should add enctype="multipart/form-data"
I am using codeigniter 3, getting error like You did not select a file to upload. in do_upload() function, but normal php function move_uploaded_file() works fine. i referred most of the answers from stackoverflow but i did not get solution.
I think it may be in wamp issue, but i did not get clearly where it is.
if this code works in your machine then it will be issue in my wamp php or Apache settings.
View: (upload.php)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php echo form_open_multipart('welcome/do_upload'); ?>
<input id="sfile" type="file" name="file">
<input type="submit" value="Submit">
<?php echo form_close(); ?>
</body>
</html>
Controller: (welcome.php)
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()) //not working
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
}
echo $basename = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],$config['upload_path'].$basename);
// move_uploaded_file() works fine
}
AS per CI 3 you need to pass name attribute in your $this->upload->do_upload('').
So you need to pass name attribute in it
$this->upload->do_upload('file');
Check size of file you have uploaded because you set $config['max_size'] = '100';. It means you will not able to upload file size for more then 100 kb
Read File Uploading Class
It could be related to a post loss due to 301-error.
Try replacing
<?php echo form_open_multipart('welcome/do_upload'); ?>
with (work around)
<form action="do_upload" method="post" enctype="multipart/form-data">
If it works, there's some error in routing configs
First load upload library and then after initialize config data.
And pass attribute name of your input file type in $this->upload->do_upload('file').
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); # Load upload library
$this->upload->initialize($config); # Initialize
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
I am using the function below to upload my image files to my server(Localhost). It is fine and image is being uploaded. But I need two image fields and hence both the images should be uploaded once the submit button is clicked. I used the function described here Codeigniter multiple file upload , but it is not working.
I get this error message
A PHP Error was encountered
Severity: Warning
Message: is_uploaded_file() expects parameter 1 to be string, array given
Filename: libraries/Upload.php
Line Number: 161
Well I cannot understand where the error is.
The function that I am using to upload single image is
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpeg|png|gif';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
//failed display the errors
}
else
{
//success
}
}
In addition I also like to ask can i change the input field name of my choice. i.e i always need to implement <input type="file" name="userfile"/> . Is it possible to change the name? I tried changing it and I get the message No file was selected, so that must mean that I cannot change it.
You have to loop through the uploaded files like it is shown in your provided link.
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpeg|png|gif';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
if (!empty($value['tmp_name'])) {
if ( ! $this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
//failed display the errors
} else {
//success
}
}
}
}
And try using HTML like this:
<input type="file" name="file1" id="file_1" />
<input type="file" name="file2" id="file_2" />
<input type="file" name="file3" id="file_3" />
You can change the names of the input fields as you like.
If you are using file multi-select, then do this:
HTML (HTML5 file input with array name allows to select multiple files):
<input type="file" name="userfile[]"/>
or
<input type="file" name="userfile[]"/>
<input type="file" name="userfile[]"/>
PHP in CodeIgniter:
// load upload library (put your own settings there)
$this->load->library('upload', array('allowed_types' => 'svg|gif|jpg|png', 'upload_path' => $GLOBALS['config']['upload_path'], ));
// normalise files array
$input_name = 'userfile'; // change it when needed to match your html
$field_names = array('name', 'type', 'tmp_name', 'error', 'size', );
$keys = array();
foreach($field_names as $field_name){
if (isset($_FILES[$input_name][$field_name])) foreach($_FILES[$input_name][$field_name] as $key => $value){
$_FILES[$input_name.'_'.$key][$field_name] = $value;
$keys[$key] = $key;
}
}
unset($_FILES[$input_name]); // just in case
foreach ($keys as $key){
$new_file = $this->upload->do_upload($input_name.'_'.$key);
// do your stuff with each uploaded file here, delete for example:
$upload_data = $this->upload->data();
unlink($upload_data['file_name']);
}
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.
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.