4

I'm following a tutorial to upload a picture from android to server and i'm using php on server side and this is the code:

<?php
    error_get_last();
    chmod("./uploadedimages/", 777);
    // Get image string posted from Android App
    $base=$_REQUEST['image'];
    // Get file name posted from Android App
    $filename = $_REQUEST['filename'];
    // Decode Image
    $binary=base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');
    // Images will be saved under 'www/imgupload/uplodedimages' folder
    $file = fopen('uploadedimages/'.$filename, 'wb');
    // Create File
    fwrite($file, $binary);
    fclose($file);
    echo 'Image upload complete, Please check your php file directory';
?>

When i try to run, it shows me the following errors:

fopen(uploadedimages/): failed to open stream: No such file or directory in...

fwrite() expects parameter 1 to be resource, boolean given in... fclose() expects parameter 1 to be resource, boolean given in...

I'm very new to php so please help me ( I've found some answers from the internet but it didn't work for me)

habib ul haq
  • 824
  • 6
  • 13
Alex41
  • 39
  • 1
  • 2
  • 4

2 Answers2

3

You need to give full path of the file in fopen()

$file = fopen($_SERVER['DOCUMENT_ROOT'].'/uploadedimages/'.$filename, 'wb');

Also make sure you have the right permissions to create a file in your server.

And since the file is not getting created in the first place, you are getting errors when you are trying to use fwrite() and fclose()

Abhinav
  • 8,028
  • 12
  • 48
  • 89
0

The failed to open stream error occurs when PHP cannot find the path you have specified. And also in think you didn't specify the file extension so use something like fopen('uploadedimages/'.$filename.'.png', 'w+'). The extension can be your preferred choice.

Gideon Appoh
  • 678
  • 1
  • 6
  • 15