Thursday, March 30, 2017

how to delete image from folder using dropzone?

I am using laravel framework 5.2. I have successfully implemented dropzone and i have also done with upload images. Now problem is that when i want to delete the image from folder it gives me error. I think my code is not right for deleting image.

Here is my add Upload image Function i have uploaded images in session:-

public function addContributorimages(Request $request){
    if($request->ajax()){
        $image=$_FILES['file'];
        if(!empty($image)){
            if($image['error']==0){
                $name = pathinfo($_FILES['file']['name']);
                $ext = $name['extension'];
                $rand=str_random(24).'.'.$ext;
                $destination = base_path() . '/public/images/ContributorImages/';
                if(is_uploaded_file($image['tmp_name'])){
                list( $width, $height, $source_type ) = getimagesize($image['tmp_name']);
                if ($width >= 10 && $height >= 10){
                    move_uploaded_file($image['tmp_name'],$destination.$rand);
                    $request->session()->put('contributorimage.'.str_random(5).'.image',$rand);
                    $images = $request->session()->get('contributorimage');
                    echo "<pre>"; print_r($images);
                }
                else{     
                        echo "Error";die;
                    }
                }
            }
        }
    }
}

This is my add Function of images Here is my dropzone code :-

Dropzone.autoDiscover = false;
    var fileList = new Array;
    var i =0;
    $("#my-awesome-dropzone").dropzone({
        method:'POST',
        maxFiles: 10,
        paramName: "file",
        maxFilesize: 10,
        addRemoveLinks: true, 
        acceptedFiles: ".jpeg,.jpg,.png,.gif",
        clickable: true,

        init: function() {

            // Hack: Add the dropzone class to the element
            $(this.element).addClass("dropzone");
            this.on("sending",function(file, xhr, formData) {
                formData.append("_token", "");
            });
            this.on("success", function(file, serverFileName) {
                fileList[i] = {"serverFileName" : serverFileName, "fileName" : file.name,"fileId" : i };
                //console.log(fileList);
                i++;

            });
            this.on("removedfile", function(file) {
                var rmvFile = "";
                for(f=0;f<fileList.length;f++){

                    if(fileList[f].fileName == file.name)
                    {
                        rmvFile = fileList[f].serverFileName;

                        if (rmvFile){
                            $.ajax({
                                type: 'POST',
                                url: '../contributor/delete-subimages/'+rmvFile,
                            });
                        }
                    }
                }

            });

        },
        url: '../contributor/add-subimages',
    });

});

My images are successfully uploaded but i want to remove the image from session as well as from folder can anyone help me how to do that Here is my delete function of image:-

public function deleteContributorImage(Request $request,$name = null){
    $imageName=explode('.',$name);
    $imageRandomName = $request->session()->get('contributorimage.'.$imageName[0].'.image');
    $destination = base_path() . '/public/images/ContributorImages/';
    if(unlink($destination.$imageRandomName)){
        $request->session()->forget('contributorimage.'.$imageName[0]);
        echo "success";
    }
    else{
        echo "failed";
    }
}

Now when i upload images it create this sesssion now i am having two images in session

Array
(
[Dkf08] => Array
    (
        [image] => whywu3dprVPKKkhUgdIMAdLQ.jpg
    )

[rH5NV] => Array
    (
        [image] => i2sZEqjMdiQHcKRyy5Km9vlu.jpg
    )

)

can anyone hlep me how to slove this issue . Thanks in advance :)



via kunal

Advertisement