Php Laravel:将Base64.png文件从控制器保存到公用文件夹

Php Laravel:将Base64.png文件从控制器保存到公用文件夹,php,laravel,laravel-4,Php,Laravel,Laravel 4,我通过Ajax向base64中的控制器发送png图像文件。我已经测试并确认控制器已收到id,但仍然无法将其保存到公用文件夹 这是我的控制器 public function postTest() { $data = Input::all(); //get the base-64 from data $base64_str = substr($data->base64_image, strpos($data->base64_image,

我通过Ajax向base64中的控制器发送png图像文件。我已经测试并确认控制器已收到id,但仍然无法将其保存到公用文件夹

这是我的控制器

public function postTest() {
        $data = Input::all();

        //get the base-64 from data
        $base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1);

        //decode base64 string
        $image = base64_decode($base64_str);
        $png_url = "product-".time().".png";
        $path = public_path('img/designs/' . $png_url);

        Image::make($image->getRealPath())->save($path);
        // I've tried using 
        // $result = file_put_contents($path, $image); 
        // too but still not working

        $response = array(
            'status' => 'success',
        );
        return Response::json( $response  );
}

这是一个容易犯的错误

您使用的公共路径不正确。应该是:

$path = public_path() . "/img/designs/" . $png_url;
另外,我会避免您发送图像的方法。查看表单中的正确上传,并使用Laravel的Input::file方法。

我已经完成了

我换了
$data->base64\u image
$\u POST['base64\u image']
,然后使用
$result=file\u put\u contents($path,$image)
而不是
Image::make($Image->getRealPath())->save($path)

但这看起来不像是一个拉威尔的方式。我想你有另一种看起来更优雅的方式请告诉我

实际上,
Input::all()
返回一个
数组
,因此您有以下几点:

$data = Input::all();
现在,您的
$data
是一个数组,而不是一个对象,因此您尝试以对象的形式访问图像,如:

$data->base64_image
所以,它不起作用了。您应该尝试使用:

$image = $data['base64_image'];

由于它(
base64\u image
)可以从
$\u POST
访问,因此
Input::file('base64\u image')
将不起作用,因为
Input::file('base64\u image')
检查
$\u文件
数组,而它不在您的案例中。

干预图像使用file\u get\u content函数获取二进制数据:
$data = Input::all();
$png_url = "perfil-".time().".jpg";
$path = public_path() . "/img/designs/" . $png_url;
$img = $data['fileo'];
$img = substr($img, strpos($img, ",")+1);
$data = base64_decode($img);
$success = file_put_contents($path, $data);
print $success ? $png_url : 'Unable to save the file.';
参考:

您的控制器应如下所示:

public function postTest() {
    $data = Input::all();
    $png_url = "product-".time().".png";
    $path = public_path().'img/designs/' . $png_url;

    Image::make(file_get_contents($data->base64_image))->save($path);     
    $response = array(
        'status' => 'success',
    );
    return Response::json( $response  );
 }

我所做的是使用基本方法

$file = base64_decode($request['profile_pic']);
            $folderName = '/uploads/users/';
            $safeName = str_random(10).'.'.'png';
            $destinationPath = public_path() . $folderName;
            file_put_contents(public_path().'/uploads/users/'.$safeName, $file);

           //save new file path into db
            $userObj->profile_pic = $safeName;
        }
我的解决办法是:

public function postTest() {
        $data = Input::all();

        //get the base-64 from data
        $base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1);

        //decode base64 string
        $image = base64_decode($base64_str);
        Storage::disk('local')->put('imgage.png', $image);
        $storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
        echo $storagePath.'imgage.png'; 
        $response = array(
            'status' => 'success',
        );
        return Response::json( $response  );
}

谢谢你的回复!我总是像这样使用public_path(),这很好。我认为问题也在于Input::file方法。所以现在我要放弃使用它:P.检查我的答案!路径不是问题,也不是必需的,只是
$path=“/img/designs/”$png_url
足够了。您的问题是
$data->base64_image
,应该是
$data['base64_image']
,请检查我的答案。谢谢您指出这一点!我正在对
$result=file\u put\u contents($path,$image)使用您的方法现在构建png图像。但文件不可读。
$data['base64\u image']
是否已经是可保存的文件?可能不是,您是否使用了
$base64\u str
?是,但仍然无法读取。仅供参考,base64_图像看起来像这样“data:image/png:base64,iVBORwOkG…”$data['fileo']代表什么?我们如何添加路径输入法。文件名应该是动态的。
$file = base64_decode($request['image']);
        $folderName = 'public/uploads/';
        $safeName = str_random(10).'.'.'png';
        $destinationPath = public_path() . $folderName;
        $success = file_put_contents(public_path().'/uploads/'.$safeName, $file);
        print $success;