0

I have this php file : UploadToServer.php that decode base 64 an image string and save it as a bitmap image, when I test it with Postman and give it a string, this error popups, I am not very familiar with php. This is the 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));
}

?>

And this is the error :

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 

2 Answers 2

2

You should use $_POST['image_encoded']. The $_POST identifier is actually an array, so the brackets should be square brackets. To test this yourself you could output print_r($_POST); once, as you'll never forget it again then.

The code would then become:

<?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));
}

?>
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry i didn't understand exactly what do you mean, so I have to try this code print_r($_POST); ?
In your code you use $_POST('image_encoded') . Normal brackets () are used for function calls. Php interprets this as if you were to call a function $_PHP() { } that you defined somewhere else (which you didn't). Therefore you must use the square brackets [] which are used for arrays, as the $_POST variable is an array.
It's a minor change but it makes a whole lot of difference. So you should use $_POST['image_encoded'] instead of $_POST('image_encoded')
0

Look the variable $filePath don't exist, the correct is $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));
}

?>

Comments