PHP - 如何将base64字符串解码为字节数组?

时间:2013-05-14 14:23:48

标签: php base64 decode

在客户端,用户发送编码图像文件。(以及项目的名称和描述) 但是,我收到的只是空档... 在服务器数据库中,我收到了带有这样的字符串的编码图像文件

/ 9J / 4AAQSkZJRgABAQAAAQABAAD / 2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQE

这应该转换为字节数组...并使用jpg文件将其保存在我的目录中。

这是我的代码。

<?php

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name']) && isset($_POST['description']) && isset($_POST['photo'])) {

$name = $_POST['name'];
$description = $_POST['description'];
$photo = $_POST['photo'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, description, photo) VALUES('$name', '$description', '$photo')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Product successfully created.";

    // echoing JSON response
    echo json_encode($response);



    //File Decoding
        $target_path = "/android_connect/";
        $base=$_REQUEST['image'];
        echo $base;
    // base64 encoded utf-8 string
        $binary=base64_decode($base);
    // binary, utf-8 bytes
        header('Content-Type: bitmap; charset=utf-8');
    // print($binary);
    //$theFile = base64_decode($image_data);
        $file = fopen($target_path, 'wb');
        $file = fopen($name, 'wb');
        fwrite($file, $binary);
        fclose($file);
    //move_uploaded_file($file,$target_path.$name);
        copy($name,$target_path.$name);

directory……';

} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

请让我知道php代码有什么问题......

0 个答案:

没有答案
相关问题