aleksov's avatar

Issue with Laravel - str_random()

I have this code to upload image with Laravel:


{
        $article['image']= 'http://nationaluasi.com/dru/content/hotelIcon.png';
        $file = array('image' => $request->file('image'));
        // setting up rules
        $rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000
         // doing the validation, passing post data, rules and the messages
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
        // send back to the page with the input data and errors
        $article['image'] = null;

        }
        else {
    // checking file is valid.
        if ($request->file('image')->isValid()) {
        $destinationPath = public_path().'/images'; // upload path
        $extension = $request->file('image')->getClientOriginalExtension(); // getting image extension
         $fileName = str_random(5).'.'.$extension; // renameing image
         $request->file('image')->move($destinationPath, $fileName); // uploading file to given path
         // sending back with message
         $article['image'] = $fileName;

         }
        else {

        }
  }

so everything is fine with image upload but when I see my /public/images folder the image have name: DiYhh.png but in database is stored: /tmp/phpVu3QOa

Why, what is the probem? Why dont store the same name in image column at database (DiYhh.png) instead /tmp/... ???

0 likes
1 reply
RoboRobok's avatar

Storing this /tmp/phpVu3QOa name in database doesn't make any sense, because this file will be deleted when request is done anyway. Where is the code that is saving to the database?

Please or to participate in this conversation.