如何使用base64在php上传图像

时间:2014-08-20 09:12:21

标签: php

您好我可以轻松上传php中的图片,如下代码

            @$file_path = "admin/alumni_image/";
            @$file_name=  basename($_FILES['photo']['name']);

            @$file_path = $file_path . basename( $_FILES['photo']['name']);
            if(move_uploaded_file(@$_FILES['photo']['tmp_name'], $file_path)) 
            {
                $response1=array();

                $query = "update alumni set photo = '".$file_name."' where alumni_id = '".$data['alumni_id']."'";
                $query_run=  mysql_query($query);
                echo mysql_error();


             } 

但是当我把图像作为base64时,我现在面临着问题。

1 个答案:

答案 0 :(得分:2)

最近我建造了类似的东西:

        define("UPLOAD_DIR", "images/signatures/");
        $signature = $_POST['signature']; // THIS IS YOUR BASE64 ENCODED STRING
        $signature = str_replace('data:image/png;base64,', '', $signature);
        $signature = str_replace(' ', '+', $signature);
        $data = base64_decode($signature);
        $file = UPLOAD_DIR . md5(microtime()) . '.png';
        $success = file_put_contents($file, $data);