【问题标题】:Decode Base-64 image string to bitmap - php将 Base-64 图像字符串解码为位图 - php
【发布时间】:2015-08-07 10:10:24
【问题描述】:

我有这个 php 文件:UploadToServer.php 解码 base 64 图像字符串并将其保存为位图图像,当我用 Postman 测试它并给它一个字符串时,这个错误弹出窗口,我对 php 不是很熟悉. 这是 UploadToServer.php :

<?php
if (isset($_POST('image_encoded'))) {
    $data = $_POST('image_encoded');
    $file_path = "C:/wamp/www/android_api/Uploads/test.jpg";
    // create a new empty file
    $myfile = fopen($filePath, "wb") or die("Unable to open file!");
        // add data to that file
    file_put_contents($filePath, base64_decode($data));
}

?>

这是错误:

Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) 
in C:\wamp\www\android_api\UploadToServer.php on line 

【问题讨论】:

    标签: php bitmap base64 encode


    【解决方案1】:

    您应该使用$_POST['image_encoded']。 $_POST 标识符 实际上是一个数组,所以括号应该是方括号。要自己测试这个,你可以输出一次print_r($_POST);,因为你再也不会忘记它了。

    然后代码将变为:

    <?php
    if (isset($_POST['image_encoded'])) {
        $data = $_POST['image_encoded'];
        $file_path = "C:/wamp/www/android_api/Uploads/test.jpg";
        // create a new empty file
        $myfile = fopen($filePath, "wb") or die("Unable to open file!");
            // add data to that file
        file_put_contents($filePath, base64_decode($data));
    }
    
    ?>
    

    【讨论】:

    • 对不起,我不明白你的意思,所以我必须试试这个代码 print_r($_POST); ?
    • 哦,我明白了,我会试试的
    • 在您的代码中使用 $_POST('image_encoded') 。普通括号 () 用于函数调用。 Php 将此解释为您要调用您在其他地方定义的函数 $_PHP() { }(您没有)。因此,您必须使用用于数组的方括号 [],因为 $_POST 变量是一个数组。
    • 这是一个很小的变化,但它会产生很大的不同。所以你应该使用 $_POST['image_encoded'] 而不是 $_POST('image_encoded')
    【解决方案2】:

    看变量$filePath不存在,正确的是$file_path:

      <?php
    if (isset($_POST['image_encoded'])) {
        $data = $_POST['image_encoded'];
        $file_path = "C:/wamp/www/android_api/Uploads/test.jpg";
        // create a new empty file
        $myfile = fopen($file_path, "wb") or die("Unable to open file!");
            // add data to that file
        file_put_contents($file_path, base64_decode($data));
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      相关资源
      最近更新 更多