File extension is change upper case to lower case in codeigniter - php

I am uploading file in codeigniter using
$this->upload->do_upload('image')
but when file moves to particular path the file extension is changed (it's changed to lower case)
for example if I upload file "profile.JPG" it's changes to "profile.jpg"

please change CI system library
default in CI upload library $file_ext_tolower = FALSE.
.system\libraries\upload.php
public $file_ext_tolower = TRUE;

do_upload does this
$config['allowed_types'] = 'gif|jpg|png'
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);
}
}
If you want to use same name / extension you send to save your file. You can use :
$upload_dir= $this->config->item("upload_dir");
$fileName = $_POST['sku_code'].".".$extension;
$filePath = $upload_dir.$fileName;
move_uploaded_file($_FILES["image-file"]["tmp_name"],$filePath );
Useful link :
https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Related

Unable to upload these formate file in codeigniter STEP, STP, IGES, IGS, SLDPRT, 3DM, SAT or X_T

Unable to upload these formate file in codeigniter STEP, STP, IGES, IGS, SLDPRT, 3DM, SAT or X_T
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf|step';
$config['max_size'] = 100000000000;
//$config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('fileUpload'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
// echo 'error';
// $this->load->view('upload_form', $error);
}else{
$this->upload->data();
}
error :- The filetype you are attempting to upload is not allowed
for those file types, just change your config of allowed_types into this to override checking extension:
$config['allowed_types'] = "*";
Before uploading your file, codeigniter check your file's extension first, but your file types are not in application/config/mimes.php so codeigniter does not know and then always return FALSE.
Another way is to add your mimes type into mimes.php, you can check out this in this link

I can't upload a markdown file by the CodeIgniter

I don't know how to set the allowed_types, and the markdown file suffix is .md. It always says:
The filetype you are attempting to upload is not allowed.
English is not my native language, so I hope this makes sense. My code:
public function do_upload()
{
$config['upload_path'] = './blog/';
$config['allowed_types'] = ''; //file's tye
$config['max_size'] = 50;
$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());
$this->load->view('houtai_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
}
You just need to allowed_types in your $config array
$config['allowed_types'] = 'md';
If you want to allow all kinds of files than you can use * like:
$config['allowed_types'] = '*';
File Uploading Class
**Customize File type allow **
$config['allowed_types'] = 'gif|jpg|png';
You can edit APPATH.'config/mimes.php' file and add 'md' => 'text/plain', to array.

How can i Upload image files only using code igniter?

hi i'm trying to upload only image files using code igniter.I need to upload only png,jpg,jpeg files.but other type files also uploaded like .txt,.mp3 etc.my code is given below.please help me to solve this issue as immediately.Thanks to all in advance.
$config['upload_path'] = './avatar/';
$config['allowed_types'] = 'gif|jpeg|png|jpg';
$config['max_size'] = '1024mb';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$ve['data'] = $this->upload->display_errors();
} else {
$this->upload->data();
}
$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);
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the //class:
$this->upload->initialize($config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('file_view', $error);
}
I think you need to send in the user file/uploaded file while trying to validate it within the if condition.
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$this->upload->data();
}

Codeigniter Rename file on upload

I'm trying to add time as the prefix of the image name along with the original name when uploading, But I couldn't figure it out. Please help me with the following code to add a prefix to my original file name when uploading.
<?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'] = 'Public/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);
}
}
}
?>
You can encrypt file name with use of CI native option:
$config['encrypt_name'] = TRUE;
OR
You can do it with your own code:
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
For some reasons, consecutive calls to the do_upload function doesn't work. It sticks to the first filename set by the first function call
$small_photo_url = $this->upload_photo('picture_small', $this->next_id.'_small ');
$medium_photo_url = $this->upload_photo('picture_medium', $this->next_id.'_medium');
$large_photo_url = $this->upload_photo('picture_large', $this->next_id.'_large ');
The filenames will all be "00001_small", "00001_small1", "00001_small2"
given the following configurations
function upload_photo($field_name, $filename)
{
$config['upload_path'] = 'Public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $filename;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())...
I think it's because this line doesn't work the second time you call it. It does not set the configurations again
$this->load->library('upload', $config);
==========================================================================
Solution to the problem faced during consecutive do_upload function calls:
// re-initialize upload library
$this->upload->initialize($config);
/* Conf */
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '';
$config['max_width'] = '';
$config['max_height'] = '';
$config['file_ext_tolower'] = TRUE;
/* Load the upload library */
$this->load->library('upload', $config);
For what you are exactly looking for, this worked for me:
$path = $_FILES['image']['name'];
$newName = "<Whatever name>".".".pathinfo($path, PATHINFO_EXTENSION);
when you use do_upload() function its rename file name if already exists and upload file
System->libraries->Upload.php
default function of do_upload() return true or false
replace
return $this->upload_path.$this->file_name;
and in another file
<input type='file' name='userfile'/>
<?php
$upload_file_name = $this->upload->do_upload('userfile');
$finalname;
if (!$upload_file_name) {
$finalname = 'default.png';
} else {
$split_data_upload_file_name = explode("/", $upload_file_name);
foreach ($split_data_upload_file_name as $i => $value) {
$finalname = $value;
}
}
?>
$config['file_name'] = $new_name;
Just add it align with the config codes.
This should be after the upload.
Process the filename you want here:
$a=$this->upload->data();
rename($a['full_path'],$a['file_path'].'file_name_you_want');
Try This:
$config['file_name'] = "yourCustomFileName";
If your $config is with this code: $config['encrypt_name'] = TRUE;
the name of your file will be forced to rename with a encrypt name, just remove the code.
Solution 1: Upload file with new customized and specific name (Note: this answer is applicable when you are submitting a unique name of a record, for example student name is a unique value in a record then you will use $_POST['student_name'])
$config['file_name'] = str_replace(' ', '_', $_POST['anyUniqueValue']);
//any Other unique value that you are using to submit with this file
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;
Solution 2: Upload file with new unique name
$config['file_name'] = time();
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;
Solution 3: Upload file with new unique name using codeigniter's default option
$config['encrypt_name'] = TRUE;
if you do multiple file upload, maybe you can try update library Upload.php
on system/libraries/Upload.php. On function do_upload()
$this->file_name = $this->_prep_filename($_file['name']);
to
$this->file_name = time().'_'.$this->_prep_filename($_file['name']);

Codeigniter : Filename didn't appear on database

I just learn to save image data to database, those are filename and path. The path is appear on database, but not with filename. Whats the problem?
This is the controller,
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);
$this->upload_model->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 the model
function upload ($config) {
$config = $this->upload->data();
$upload_data = array(
'path' => $config['full_path'],
'nama_foto' => $config['file_name']
);
$this->db->insert('tb_picture', $upload_data);
}
and the table
what should i do?
thank you before.
before reading this please try it again yourself, or try any kind of viedotutorial that is on the web http://net.tutsplus.com/sessions/codeigniter-from-scratch/
controller function should look like this
function do_upload()
{
$config['upload_path']='./uploads/'; //needs to set uploads folder CHMOD 0644
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$field_name = "userfile"; //name tag in our HTML form in case you want to change it
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($field_name)) //upload happens
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
//succesful upload get data from upload and use them with our model
$upload_data = $this->upload->data();
$this->upload_model->upload($upload_data);
}
}
model function
function upload ($data) {
if (empty($data) || $data === FALSE) return FALSE;
$insert_data = array(
'path' => $data['full_path'],
'nama_foto' => $data['file_name']
);
$this->db->insert('tb_picture', $insert_data);
return $this->db->insert_id(); //returns last inserted ID
}
Please note that your model was totaly "wrong" you used upload function in it, try to pass data only to it so model can process it.

Categories