Home  >  Article  >  PHP Framework  >  How to delete pictures in thinkphp image

How to delete pictures in thinkphp image

藏色散人
藏色散人Original
2022-12-05 10:32:011073browse

thinkphp image How to delete images: 1. Open the front-end code file; 2. Use the Ajax code "success:function(data) {...}" to achieve partial refresh; 3. Open the tp back-end code file , and delete the image data through the "public function delete_image(){...}" method.

How to delete pictures in thinkphp image

The operating environment of this tutorial: Windows 7 system, ThinkPHP version 5, Dell G3 computer.

thinkphp imageHow to delete pictures?

ThinkPHP5 uses ajax to asynchronously delete images

I have been writing code for a year or two, and I have some experience. Typing code is always a tiring job. It’s not so easy to stand on other people’s shoulders and fix the wheel. That’s all, let’s continue repairing the wheels.

The biggest advantage of AJAX is that it can exchange data with the server and update part of the web page content without reloading the entire page.

ThinkPHP5 uses ajax to asynchronously delete images. The entire page address remains unchanged and partial refresh is achieved.

Code display:

<script type="text/javascript">
    function delete_image(o){
        if(!confirm(&#39;确定删除图片?&#39;)){
            return false;
        }
        var url="{:url(&#39;delete_image&#39;)}";
        var imgsrc=$(o).attr(&#39;id&#39;);
        $.ajax({
            url : url,
            type : "post",
            dataType : "json",
            data : {
                imgsrc:imgsrc
            },
            //如果返回成功
            success : function(data) {
                if(data.type == 1){
                    //执行删除操作
                    $(o).remove();                                                                    
                    alert(&#39;删除成功!&#39;);
                }else if(data.type == 2){
                    alert(&#39;删除失败!&#39;);
                }else{
                    alert(&#39;删除失败!&#39;);
                }
            }
         });
     }
</script>

Submit to the background method:

public function delete_image(){
    //接收获取的值
    $imgsrc = input(&#39;imgsrc&#39;);
    //拼接链接地址
    $imgsrc = DEL_UEDITOR.$imgsrc;
    if(file_exists($imgsrc)){
        if(@unlink($imgsrc)){
            //删除成功
            echo json_encode(array(&#39;type&#39; => 1));
        }else{
            //删除失败
            echo  json_encode(array(&#39;type&#39; => 2));
        }
    }else{
        //如果不存在,删除失败
        echo json_encode(array(&#39;type&#39; => 3));
    }
}

How to delete pictures in thinkphp image

Click the delete button and a pop-up box will be displayed to prompt whether the deletion is successful or failed. !

Recommended learning: "thinkPHP Video Tutorial"

The above is the detailed content of How to delete pictures in thinkphp image. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn