从服务器删除图像

时间:2016-08-08 14:44:07

标签: php laravel-5

我正在尝试从服务器中删除数据库中的记录以及该记录的上传图像。我在控制器中有此功能。

public function delete(Request $request)
    {
        if($request->ajax())
        {
            $id=$request->get('id');
            if($id)
            {
                $delete=Category::where('id',$id)->first();
                $delete->delete();
                $imgfile='C:\xampp\htdocs\larapro\public\newuploads\<?php echo $delete->image;?>';
                unlink($imgfile);
                echo json_encode(TRUE);die;
            }
        }
        echo json_encode(FALSE);die;
    }
  

unlink(C:\ xampp \ htdocs \ larapro \ public \ newuploads \&lt;?php echo   $ delete-&gt; image;?&gt;):结果太大

如果我使用

$imgfile="C:\xampp\htdocs\larapro\public\newuploads\{$delete->image}";

显示:

  

取消关联(C:mpp \ htdocs \ larapro \ public \ ewuploads {1470667358.png}):   参数无效

我只是想知道为什么链接中的x和n缺失。

2 个答案:

答案 0 :(得分:1)

 public function delete(Request $request)
        {
            if($request->ajax())
            {
                $id=$request->get('id');
                if($id)
                {
                    $delete=Category::where('id',$id)->first();

                    $imgfile="C:\xampp\htdocs\larapro\public\newuploads\$delete->image";
                    //perform the delete after using the image/filename
                    $delete->delete();

                    unlink($imgfile);
                    echo json_encode(TRUE);die;
                }
            }
            echo json_encode(FALSE);die;
        }

你可以用单引号替换双打,然后使用删除变量的内容吗?

答案 1 :(得分:0)

在这种情况下,

使用斜杠(/)代替反斜杠():

$imgfile="C:/xampp/htdocs/larapro/public/newuploads/{$delete->image}";

或者逃避转义字符,

$imgfile="C:\\xampp\htdocs\larapro\public\newuploads\{$delete->image}";

并且缺少单词x和n,因为\ x和\ n分别是转义符和换行符。