0

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", "{{ csrf_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 :)

Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45
kunal
  • 4,122
  • 12
  • 40
  • 75

1 Answers1

1

you have to create one hidden filed for that and when you remove file from dropzone than that file name should be save in that hidden filed.

myDropzone.on('removedfile', function (file) {

        var hidden_filed= document.getElementById('hidden_filed').value;
        if (alreadyRemove == "") {
            $('#deleteImage').val(file.name);
        } else {
            $('#deleteImage').val(hidden_filed+ ',' + file.name);
        }
    });

after that get that field as POST data in controller. From file name you can delete Image as usual.

Pramod Gharu
  • 1,105
  • 3
  • 9
  • 18
  • i don't understand your conditions can you please elaborate how do i use the remove file function where i have to define the deleteImage id – kunal Mar 31 '17 at 04:05
  • first you have to create a hidden filed in your form and after that when remove image from dropzone you have to send that file name to hidden filed as CSV. after form submit you can get POST data in your controllers from that you can delete image using CSV data. –  Apr 03 '17 at 06:27