Codeigniter Cambiar el nombre del archivo en la carga


Estoy tratando de agregar el tiempo como el prefijo del nombre de la imagen junto con el nombre original al cargar, pero no pude entenderlo. Por favor, ayúdame con el siguiente código para agregar un prefijo a mi nombre de archivo original al cargar.

<?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);
        }
    }
}
?>
Author: Dilukshan Mahendra, 2014-02-16

5 answers

Puede cifrar el nombre del archivo con el uso de la opción nativa de CI:

$config['encrypt_name'] = TRUE;

O

Puedes hacerlo con tu propio código:

$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
 68
Author: Kumar V,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-16 13:46:18

Por algunas razones, las llamadas consecutivas a la función do_upload no funcionan. Se adhiere al primer nombre de archivo establecido por la primera llamada a la función

$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 ');

Los nombres de archivo serán todos "00001_small", "00001_small1", " 00001_small2" dadas las siguientes configuraciones

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())...

Creo que es porque esta línea no funciona la segunda vez que la llamas. No establece las configuraciones de nuevo

$this->load->library('upload', $config);

========================================================================== Solución al problema al que se enfrenta durante llamadas consecutivas a la función do_upload:

// re-initialize upload library
$this->upload->initialize($config);
 8
Author: Carmela,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-23 09:49:12

Para lo que estás buscando exactamente, esto funcionó para mí:

$path = $_FILES['image']['name'];
$newName = "<Whatever name>".".".pathinfo($path, PATHINFO_EXTENSION); 
 2
Author: Heshan Rajapaksha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-04 03:02:53
$config['file_name'] = $new_name;

Simplemente agrégalo alineado con los códigos de configuración.

 0
Author: Jesus Erwin Suarez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-04 18:30:00

Esto debería ser después de la carga.

Procese el nombre de archivo que desea aquí:

$a=$this->upload->data();         
rename($a['full_path'],$a['file_path'].'file_name_you_want');
 0
Author: user2955330,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-06 09:41:10