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