micky
micky

Reputation: 317

Removing image from server

I am trying to remove the records from the database as well as the uploaded image for that record from the server.I have this function in controller.

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\<?php echo $delete->image;?>): Result too large

if i use

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

it displays:

unlink(C: mpp\htdocs\larapro\public\ ewuploads{1470667358.png}): Invalid argument

and i am just wondering why x and n in the link are missing.

Upvotes: 0

Views: 67

Answers (2)

Sanzeeb Aryal
Sanzeeb Aryal

Reputation: 3266

use slash (/) instead of backslash () in this case:

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

Or escape the escape character like,

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

and the words x and n are missing because \x and \n are escape and new line character respectively.

Upvotes: 0

mgilberties
mgilberties

Reputation: 454

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

Can you substitute your single quotes for doubles and then use the contents of the delete variable?

Upvotes: 1

Related Questions