Mitch-B's avatar

Saving File Input Name to database

I am trying to save a file (image) to my database. I am able to upload, move the image and save the name to my database. The problem I am having is that when i try to save the original file name to my database it only saves the temporary name. How would I save the original name to my database? Thanks.

0 likes
17 replies
nolros's avatar

You need to use the following file methods

                'name'                => $file->getClientOriginalName(),

further info you can get from the file ($uploadFile can be Request::file() or however you load it up):

                'size'                => $uploadFile->getClientSize(),
                'mime_type'           => $uploadFile->getClientMimeType(),
                'original_name'       => $uploadFile->getClientOriginalName(),
                'original_extension'  => $uploadFile->getClientOriginalExtension(),
                'original_created_at' => $uploadFile->getCTime(),
                'original_updated_at' => $uploadFile->getATime(),
Mitch-B's avatar

I used getClientOriginalName() but it still stored it as a temporary name. Here is my code: Input::file('portfolio_image')->move('/images/portfolio/',Input::file('portfolio_image')->getClientOriginalName()); $this->portfolio->save();

When I check my database it saves it as: C:\xampp\tmp\php2EA9.tmp

Now for some reason it wont store it where I want it too. Not sure what is going on here.

nolros's avatar

You need to get the original name before you move. Once you move it the Apache temp name will be original name as the file move is a destructive action. So it would be as follows:

// get original name from file
$getOriginalFileName = Input::file( 'portfolio_image' )->getClientOriginalName(); 

// then move 
Input::file( 'portfolio_image' )->move( '/images/portfolio/', $getOriginalFileName );
2 likes
Mitch-B's avatar

I tried what you suggested but it still stores it as C:\xampp\tmp\php84B5.tmp in the DB.

jekinney's avatar

You have to declare the public directory when using wamp, mamp etc. Otherwise it doesn't know where to put it (fails the move and goes to default).

I forget the format off the top of my head.

Mitch-B's avatar

it's public_path().'/path/to/image_dir/'

1 like
nolros's avatar

Did that work for you?

Yes, I should have mentioned I have a directory service manager that builds the path for me. Example, :

getRootPath = public_path() . '/'. $this->getEnvFilePath() .'/' . $DirUuid;

Mitch-B's avatar

Unfortunately it did not. It still saves the name of the file as C:\xampp\tmp\phpE5E6.tmp in the database.

Mitch-B's avatar

When i return $original_file_name it returns the files original name but it will not save the file name as the original name.

nolros's avatar

Sorry wrong post lol ... here is the answer. My code I use for move below. You need to ensure you passing it passing full path for target which would be

EDIT adding original path code if you don't have it

// replace /files/ with your directory image path

use Illuminate\Support\Facades\File;

$setTargetDirectory = public_path() . '/files/' . $getOriginalFileName;

$setOrigPath =  Input::file('name of file here')->getRealPath(),


    public function moveFile($setOrigPath, $setTargetDirectory)
    {

        if(!empty($setTargetDirectory) && !empty($setOrigPath))
        {
            return File::move($setOrigPath, $setTargetDirectory);

        }

        return false;

    }
thepsion5's avatar

Do you really NEED to store the original client name? I've always stored it under a new semantically-relevant filename. Then I don't need to store the original name because I can look it up based on the semantic filename.

For example, the avatar image for a user with an ID of 42 might be at /app/public/img/avatars/user-42-avatar.jpg.

1 like
Mitch-B's avatar

Moving the file is not the issue, it is storing the file name in a database table. Yes, saving the original file name is important for this project I am doing.

EDIT: I would change the name of the original file (for other projects) but even if I do change the name it wouldn't save the name in the database.

Mitch-B's avatar

$original_file_name = Input::file( 'portfolio_image' )->getClientOriginalName(); Input::file('portfolio_image')->move(public_path().'/images/portfolio',$original_file_name); $this->portfolio->image_file_name = $original_file_name; $this->portfolio->save();

The code above moves the image to the directory i want. It does not save the original name to my DB.

nolros's avatar

Not sure i really get what you doing there, but here is the clean way to save a file:

    // create an instance of your file file DB model / class
    $file = new YourFileModel();

    // then write to the fields with the syntax $file->column_name = $value,
    $file->file_original_name = $getOriginalFileName;
    $file->path = $getOriginalPath;
    // etc - extension, 

    // save the instance to DB
        $file->save();

btw, from a syntax don't use DB column names for variables because $original_file_name is being confused with a column name, rather use camelCase i.e. $getOriginalName . That way you know what is a var and what is column. There is magic in Eloquant that does mapping, so sometimes it will match a var to a column, but in my opinion will add a lot of headaches.

lorvent's avatar
if ($file = Input::file('pic'))
        {
            $fileName        = $file->getClientOriginalName();
            $extension       = $file->getClientOriginalExtension() ?: 'png';
            $folderName      = '/uploads/users/';
            $destinationPath = public_path() . $folderName;
            $safeName        = str_random(10).'.'.$extension;
            $file->move($destinationPath, $safeName);
        }

$safeName is what you save into db

Please or to participate in this conversation.