图像未存储在Laravel的文件夹中

时间:2019-07-06 04:25:44

标签: php html sql laravel

我有一个表单,当用户填写该表单并提交时,数据将进入数据库,而我的邮件ID则是我的控制器

namespace App\Http\Controllers;

use App\Mail\SendEmail;
use App\Register;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Mail;
use Session;
use Storage;

class RegController extends Controller
{
    public function store(Request $request)
    {
        $data = new Register;
        $data->firstname = $request->firstname;
        $data->lastname = $request->lastname;
        $data->mobilenumber = $request->mobilenumber;
        $data->experience = $request->experience;
        $data->designation = $request->designation;
        $data->confirmation = $request->confirmation;
        $data->company = $request->company;
        $data->address = $request->address;
        $data->addressline2 = $request->addressline2;
        $data->city = $request->city;
        $data->state = $request->state;
        $data->pin = $request->pin;
        $data->country = $request->country;
        $data->Whatsapp = $request->Whatsapp;
        $data->alternatenumber = $request->alternatenumber;
        $data->Email = $request->Email;
        $data->CompanyRegNumber = $request->CompanyRegNumber;
        $data->Date_Incorporation_orBusi_Stp = $request->Date_Incorporation_orBusi_Stp;
        $data->GSTIN = $request->GSTIN;
        $data->Services_Offered = $request->Services_Offered;
        $data->Annual_Turnover = $request->Annual_Turnover;
        $data->Declaration = $request->Declaration;
        $data->Fee = $request->Fee;
        $data->termsandconditions = $request->termsandconditions;
        $data->Mode_of_Payment = $request->Mode_of_Payment;
        $data->Recommended_by_A = $request->Recommended_by_A;
        $data->Recommended_by_B = $request->Recommended_by_B;

        if ($request->hasFile('Photo')) {
            $image = $request->file('Photo');
            $filename = time().$image->getClientOriginalName();
            $destinationPath = public_path('uploads/Photo');
            Storage::disk('public')->put($filename, File::get($image));
            $data->Photo = $filename;
        }
        if ($request->hasFile('Receipt')) {
            $image = $request->file('Receipt');
            $filename = time().$image->getClientOriginalName();
            $destinationPath = public_path('uploads/Receipt');
            Storage::disk('public')->put($filename, File::get($image));
            $data->Receipt = $filename;
        }
        if ($request->hasFile('Signature')) {
            $image = $request->file('Signature');
            $filename = time().$image->getClientOriginalName();
            $destinationPath = public_path('uploads/Signature');
            Storage::disk('public')->put($filename, File::get($image));
            $data->Signature = $filename;
        }

        $data->save();
        Session::flash('registered', 'reg');

        $name = $request->firstname." ".$request->lastname;
        $subject = "TEIA";
        $message = $data->id;
        $photo = $data->Photo;
        $sign = $data->Signature;
        $recipt = $data->Receipt;
        $email = 'teiamembers@gmail.com';

        // ->cc(['name1@domain.com','name2@domain.com'])
        Mail::to($email)->send(new SendEmail($subject, $message, $photo, $sign, $recipt));

        return redirect('formdata');
    }
}

我的数据库字段

enter image description here

我已经完成以下代码来显示PHP中的值

<?php
$servername = "moluguz.accountsupportmysql.com";
$username = "teia";
$password = "bolsterking";
$dbname = "teia";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * from registers ORDER BY ID DESC limit 1 ";
$result = $conn->query($sql);

if (!empty($result) && $result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

在电子邮件中,我获取了包括图像在内的所有数据。但是在HTML网站上,当我试图显示值时,正在显示除图像之外的所有数据,并且当我检查上载文件夹时,没有图像被存储。谁能告诉我我的代码有什么问题...?

1 个答案:

答案 0 :(得分:0)

您的图像不可见,因为,在数据库中,您仅存储图像的文件名。

要上传图片,请尝试以下操作:

您可以在“ config / filesystem.php”文件中配置文件上传路径。

在您的代码中,您使用的是“公共”磁盘,因此在您的filesystem.php文件中进行以下更改:

'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ] 

您可以使用以下代码上传文件:

$image = $request->file('photo');
$filename = time().$image->getClientOriginalName();
Storage::disk('public')->put('uploads/Photo/'.$filename, $image);

这会将您的文件上传到“存储/应用/公共/上传/照片”文件夹中。

要从存储文件夹访问文件,您需要在公用文件夹中创建符号链接。您可以使用以下命令创建它:     'php artisan storage:link'

您可以查看laravel文档here

我希望它会有所帮助。