I have declared enctype="multipart/form-data"
my input type
<textarea class="input-style" name="blog2" placeholder="Para 2"></textarea>
<input type="file" name="blog_img" size="chars" class="upload-file" />
my controller:
function upload()
{
//set preferences
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'txt|pdf';
$config['max_size'] = '100';
//load upload class library
$this->load->library('upload', $config);
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('upload_file_view', $upload_error);
}
else
{
// case - success
$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('upload_file_view', $data);
}
}
Perhaps it might be a file upload size issue.
You have to check your PHP configurations on php.ini file. If your upload is constantly being interrupted you either need to increase the maximum upload size or the time before your PHP script times out. Check this variables:
post_max_size
upload_max_filesize
max_execution_time
You can either increase in your PHP configuration file or increase in your script using the ini_set function. Check the manual for more details. PHP Manual
input type='file' needs to be named 'userfile' by default
https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
You can change the default to yours at
$this->upload->do_upload('filename')
change this to
$this->upload->do_upload('your file input name')
You have
if (!$this->upload->do_upload('filename'))
but in your file input name is blog_img, so us that instead of filename
if (!$this->upload->do_upload('blog_img'))
Related
when i upload a file using do_upload() in code igniter, it uploads the file but it renames it with underscore(_) in the place of blank spaces.
file name to be uploaded: A B C.pdf
file that uploads: A_B_C.pdf
code in controller to upload file:
//$filefeild is the name of folder in which file is to be saved
public function upload_Files($filefeild)
{
//set preferences
$config['upload_path'] = 'assets/bcc/'.$filefeild."/";
$config['allowed_types'] = 'doc|docx|xml|pdf|image|excel|jpg|png|jpeg';
$config['max_size'] = 0;
$config['filname'] = $filenamee = $_FILES[$filefeild]['name'];
$this->upload->initialize($config);
if (!$this->upload->do_upload($filefeild))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
}
else
{
// case - success
$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>';
$filname = $upload_data['file_name'];
return $filenamee;
}
}
IF you see codeigniter's function exist here, you will find following code,
// Remove white spaces in the name
if ($this->remove_spaces === TRUE)
{
$this->file_name = preg_replace('/\s+/', '_', $this->file_name);
}
Which means codeigniter removes spaced and adds _ by default which can be changed by setting config variable. like $config['remove_spaces'] = FALSE;
Secondly if you want your file should be saved with underscors and you get actual name by which file is being saved then in your code you have to make a slight change as described below.
your current code
$config['filname'] = $filenamee = $_FILES[$filefeild]['name'];
Change it to
$config['filname'] = $_FILES[$filefeild]['name'];
and in success scenario get file name from $this->upload->data()
Note: you can find answere here aswell.
I want a method for validating image files.
I have tried validating using image type and its formats and size. I need another validation with high accuracy.
I am asking because my site has hacked by an uploaded image file containing code. The hacker has written code in .png format and uploaded it as an image in my site. From that moment he started hacking my site.
Kindly help me to get a high validation for an image file using its header.
I have validation like below:
function upload()
{
//set preferences
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'txt|pdf';
$config['max_size'] = '100';
//load upload class library
$this->load->library('upload', $config);
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('upload_file_view', $upload_error);
}
else
{
// case - success
$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('upload_file_view', $data);
}
}
You have allowed filetypes of txt and pdf. You should use
$config['allowed_types'] = 'gif|jpg|png';
If it is images you are uploading.
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)
i am trying to upload a mp4 file with CodeIgniter but it is not working.
it shows me an error message.
you didn't select a file to upload
and the same code is working fine with .png type images.
i changed my php.ini max upload size and post data size as per my requirement.I also added mime type for mp4 into mime.php file of the CodeIgniter:
'mp4' => 'video/mp4',
i also tried
'mpeg4' => 'video/mpeg4',
here is my controller code
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'mp4|flv|png';
$config['max_size'] = '500000';
$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);
}
}
so please suggest some solution for this issue.
-Thanks
There are 2 possibilities:
Do you name input field like this?
<input type="file" name="userfile" />
If not, you have to specify in.
$this->upload->do_upload()
the name of file input.
2.Is your form like this?
<form name="myForm" method="post" action="controller/function" enctype="multipart/form-data">
You can read more here http://codeigniter.com/user_guide/libraries/file_uploading.html
i got the solution the code is correct and working fine.
this the issue with particular mp4 file.
i need to check the mime type of that file.
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.