0

Hello I have PHP script to retrieve image and file name. I have got image and file name successfully. There is no problem about that. And after converting to the binary I want to save it as image on my server. But I am unable to save that image. Here is my PHP script.

<?php

    // 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\n';

?>
7
  • do you have error reporting turned on? could this be a permissions issue ? Commented Mar 9, 2015 at 17:13
  • no there is no any permission issue. I have tested $binary and $filename. It works. Commented Mar 9, 2015 at 17:15
  • Is there any error message that you are getting ? Commented Mar 9, 2015 at 17:17
  • No...I am getting 'Image upload complete, Please check your php file directory' Commented Mar 9, 2015 at 17:18
  • that is because your code will display that message irrespective of what happens. Commented Mar 9, 2015 at 17:19

1 Answer 1

1

Here is a simple code which I use to store images on my webserver, It doesn't convert images to binary, but it does the job of storing images onto the server.

You should create a uploads folder and give full permissions such as 777 to it.

<?php
echo $_FILES['image']['name'] . '<br/>';


//ini_set('upload_max_filesize', '10M');
//ini_set('post_max_size', '10M');
//ini_set('max_input_time', 300);
//ini_set('max_execution_time', 300);


$target_path = "uploads/";

$target_path = $target_path . basename($_FILES['image']['name']);

try {
    //throw exception if can't move the file
    if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
        throw new Exception('Could not move file');
    }

    echo "The file " . basename($_FILES['image']['name']) .
    " has been uploaded";
} catch (Exception $e) {
    die('File did not upload: ' . $e->getMessage());
}
?>

HTML which links to the php file (for your reference)

<html>
    <head>
        <title>Upload</title>
    </head>
    <body>
        <form enctype="multipart/form-data" action="fileUpload.php" method="POST">            
            Select a file<input name="image" type="file" /><br />
            <input type="submit" value="Upload File" />
        </form>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.