3

I want to upload an image and retrieve in from database when i need this.My code is perfectly working to upload image but it's path did not save in database.In database it's showing Null.Here is my code:

    if ($request->hasFile('profilePic')) {
        $file = array('profilePic' => Input::file('profilePic'));
        $destinationPath = 'img/'; // upload path
        $extension = Input::file('profilePic')->getClientOriginalExtension(); 
        $fileName = rand(11111,99999).'.'.$extension; // renaming image
        Input::file('profilePic')->move($destinationPath, $fileName);
    }else{
        echo "Please Upload Your Profile Image!";
    }
    $profile->save();

My Question:

  • How to save image path into database?
  • And how to retrieve image from database?

Updated: Profile.php

class Profile extends Model
{
    protected $table = "profiles";
    public $fillable = ["firstName","lastName","middleName","DOB","gender","featuredProfile","email","phone","summary","profilePic"];
}
2
  • Can you post your profile model file? Can't you just use something like $profile->image_location = $destinationPath . '/' . $filename; Commented Jun 2, 2016 at 5:24
  • @Brett I have updated my question. Commented Jun 2, 2016 at 5:28

3 Answers 3

2

In your code there should be like this:

$profile->profilePic = $fileName;

& then

$profile->save();
Sign up to request clarification or add additional context in comments.

Comments

1

1.How to save image path into database?

To save your full image path in a database field, the code is:

$profile->profilePic = $destinationPath.$fileName;
$profile->save();


2.And how to retrieve image from database?

In your view file using blade engine, write HTML code like this. In the image src you have to provide profilePic data as shown in the below HTML code:

<img src="{{asset($profile->profilePic)}}"/>

Comments

0

try this

$path = $file->getRealPath();;
    $pos = strpos($path,'/public/');
    if ($pos !== false) {
        $path = substr($path, $pos + 1);
    }
    $input['file'] = $path;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.