Codeigniter renaming image file is not working - php

I am using codeigniter and I'm trying to upload image files.
Everything is working fine, but I am unable to change the uploaded file name to a custom name.
I've tried everything for the past 5 hours, nothing is working.
If anyone can help, it would be great.
I just need to rename the uploaded file to custom_name.jpg
Controller file
public function uploadimage() {
$config['overwrite'] = TRUE;
$config['file_name'] = 'custom_name.jpg';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config['upload_path'] = './uploads/';
$config["max_size"] = 1024;
$config["max_width"] = 400;
$config["max_height"] = 400;
$this->load->library('upload', $config);
if(!$this->upload->do_upload()) {
$this->data['error'] = $this->upload->display_errors();
} else {
echo "success";
}
}
VIEW file
<?php echo form_open_multipart('admin/uploadimage'); ?>
<?php echo form_upload('userfile'); ?><br />
<?php echo form_submit('upload', 'Upload');?>
<?php echo form_close(); ?>

just remove custom extension from file_name and append extension of file uploaded.
public function uploadimage() {
$config['overwrite'] = TRUE;
$config['file_name'] = 'custom_name';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config['upload_path'] = './uploads/';
$config["max_size"] = 1024;
$config["max_width"] = 400;
$config["max_height"] = 400;
$this->load->library('upload', $config);
if(!$this->upload->do_upload()) {
$this->data['error'] = $this->upload->display_errors();
} else {
echo "success";
}
}
You can also use rename function to rename file after successful upload.
Also check these.
Upload directory is writable and the path can be absolute or relative.
Set preferences by calling the initialize() method, if you auto-load the class $this->upload->initialize($config)
You have some restriction on file size, also check your image size.

Related

Files not getting stored in correct folder

I have a form in CodeIgniter that allows the user to upload 2 separate files. At the backend I want these files to get stored in the different folder. In the controller i have written the upload code
public function upload()
{
/**Start uploading file**/
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
echo $error;
}
else
{
$data = $this->upload->data();
echo $file = $data['file_name']; //name of file
}
/**End uploading file**/
/**Start uploading img**/
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('img'))
{
$error1 = array('error' => $this->upload->display_errors());
echo $error1;
}
else
{
$data1 = $this->upload->data();
echo $img = $data1['file_name']; //name of img
}
/**End uploading img**/
}
The images are getting uploaded but they are getting uplaoded to same folder. Can anyone please tell how i can make the files get saved in seperate folders
in the second load , you need to initialiaze the loaded library because the load method don't initialize it :
$this->upload->initialize($config2);

Codeigniter large video is uploading fine but audio is not uploading

In my controller I am trying to add audio and video, I changed my php.ini file and extended max_file_size, post_size etc.
When I upload a video of 25M, it is successfully getting upload
but when I try to upload an audio file of 4MB then it got failed.
Then again I tried to upload an audio file of 433KB, it got successfully upload.
HERE is my code below and I am not able to find what is the reason its not working
if(!empty($_FILES['audio_upload']['name'])){
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'mp3|wmv|mp4';
$config['max_size'] = '0';
$config['file_name'] = $this->input->post('sub_category_name');
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('audio_upload')){
$uploadData = $this->upload->data();
$audio_upload = $uploadData['file_name'];
}
else{
$audio_upload = '';
}
}
if( !empty($audio_upload)){
$media_link = 'uploads/'.$audio_upload;
}
else{
$media_link = $this->input->post('media_link');
}
//**************************************END
//**************************************Video Upload
if(!empty($_FILES['video_upload']['name'])){
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'mp3|wmv|mp4';
$config['max_size'] = '0';
$config['file_name'] = $this->input->post('sub_category_name');
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('video_upload')){
$uploadData = $this->upload->data();
$video_upload = $uploadData['file_name'];
}
else{
$video_upload = '';
}
}
if( !empty($video_upload)){
$video_link = 'uploads/'.$video_upload;
}
else{
$video_link = $this->input->post('media_link');
}

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

Codeigniter can't read file name of uploaded file

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"

unable to upload a file in codeigniter

whenever i try to upload a file by pressing 'upload file' after choosing, it directs me to a blank page.tried the official documentation as well as various videos on youtube.
public function index()
{
$this->load->view('upload_form');
}
//upload_form
<?php
echo form_open_multipart(base_url()."index.php/home/upload_file");
echo form_upload("file");
echo form_submit("upload","Upload file");
?>
public function upload_file()
{
if($this->input->post("upload")===false)
return;
$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("file"))
{
$this->load->view("error");
}
else
{
$this->load->view("success");
}
}
error and success pages are just simple texts.
($this->input->post("upload")===false)
will not work. Use this instead:
if(!empty($_FILES['upload']['name'])) {
...
}

Categories