file_get_contents获取参数并用file_put_contents保存图片

file_get_contents获取参数

$_POST只能接收Content-type为application/x-www-form-urlencoded 或 multipart/form-data 的参数,不能接收Content-type为text 或 xml 的参数.

file_get_contents因为可以读取到请求体(request body)的内容,所以可以接收post传递的信息.
但Content-type为multipart/form-data时,file_get_contents则接收不到post参数.

以下示例用file_get_contents获取参数,并保存为图片

		$all = file_get_contents("php://input");
		//因为传过来是json,所以要decode
        $all = json_decode($all,true);
        
        $img = $all['img'];
        $user_id = $all['user_id'];
		//存储路径这样会默认从public下开始找(后缀png我给定死了)
        $file_path = 'storage/aaa/'.$user_id.'.png';
		//判断如果当前文件存在则删除
        if(file_exists($file_path)){
            unlink($file_path);
        }
		//将传过来base64过后的图片解密,并保存
        $img = file_put_contents($file_path,base64_decode($img));

你可能感兴趣的:(PHP)