user3359775
user3359775

Reputation: 119

Laravel 4 - Change (upload imagename)

I'd like to rename images using random name. Do I have to use some logic encryption?

I have the function in my Controller postAdd to upload an image and save some information:

    public function postAdd()
    {
     $path = 'uploads/noticias';
     $file = Input::file('archivo'); 
     $archivo=$file->getClientOriginalName();
     $extension =$file->getClientOriginalExtension(); 
     $tamano=$file->getSize();
     $upload = $file->move($path, $archivo);
     if($upload)
     {
         $inputs=Input::All();
         $n= new Noticias;
        $n->titulo = $inputs["titulo"];
        $n->contenido=$inputs["contenido"];
        $n->seo_slug=$inputs["titulo"];
        $n->fecha=date("Y-m-d");
        $n->foto=$archivo;
        $n->save();
        return Redirect::to('add');
     }else
     {
        return Redirect::to('add');
     }
    }

How can I do it?

Upvotes: 0

Views: 3412

Answers (2)

Andreyco
Andreyco

Reputation: 22872

Filename should contain extension as well.
To create filename, generate random string and append extension from uploaded file. Preferably, lower the character case (or do upprecase , but keep it the same) In the example I posted for you I use value function, which return the result from whatever you pass into - in this case I pass anonymous function, which generates the filename.

public function postAdd()
{
    $path = 'uploads/noticias';
    $file = Input::file('archivo'); 
    $archivo = value(function() use ($file){
        $filename = str_random(10) . '.' . $file->getClientOriginalExtension();
        return strtolower($filename);
    });
    $tamano = $file->getSize();
    $upload = $file->move($path, $archivo);
    if($upload) {
        $inputs=Input::All();
        $n= new Noticias;
        $n->titulo = $inputs["titulo"];
        $n->contenido=$inputs["contenido"];
        $n->seo_slug=$inputs["titulo"];
        $n->fecha=date("Y-m-d");
        $n->foto=$archivo;
        $n->save();
        return Redirect::to('add');
    } else {
        return Redirect::to('add');
    }
}




Edit

Explanations, as requested in comment...

value function:
To be clear, it is not the native PHP function. It is one of Laravel's helper functions - info here.
This is its source. It either executes closure (anonymous function) if closure is passed, or just returns what is passed (value).

 /**
 * Return the default value of the given value.
 *
 * @param  mixed  $value
 * @return mixed
 */
function value($value)
{
 return $value instanceof Closure ? $value() : $value;
}

I chose to use this to make code more readable. $filename variable used inside the closure, is modified and returned - nothing from closure is needed outside of it.
Plus, you can use $filename variable in scope of postAdd function and they just don't mix, it is not possible their values are overwriten by accident, and so on...

Upvotes: 3

halkujabra
halkujabra

Reputation: 2942

'str_random'

Generate a random string of the given length.

$string = str_random(40);//random string of length 40

http://laravel.com/docs/helpers#strings

Upvotes: 0

Related Questions