8Answers
  • 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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;
  • 95
Reply Report

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);
  • 8
Reply Report
      • 2
    • Found the solution to this problem. I'll put here in case if anyone looking for the solution. Solution is simple... Load the library in the constructor $this->load->library('upload'); Then initialize the function each time before upload, $this->upload->initialize($config); it'll work...
        /* 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);
  • 2
Reply Report

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

?>
  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions