-2

I have used base 64 method to insert image into database.

Client side:

NSData *data = nil;
NSString *imageData = nil;

if (insertimageview.image != nil) {
           UIImage *image = insertimageview.image;
           data = UIImagePNGRepresentation(image);
           imageData = [data base64EncodedStringWithOptions:0];
           NSString *url=[[NSString alloc]initWithFormat:@"http://localhost/rk.php?sr=%@",imageData];
           NSData *dat=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:url]];
           NSString *echoo=[[NSString alloc]initWithData:dat encoding:NSUTF8StringEncoding];
           NSLog(@"%@",echoo); }

In my server side I have decoded the string with base64 function.

Server side:

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="q"; // Database name
        \
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$cat=$_GET["sr"];
$catt="rrrr";
$img = @imagecreatefromstring(base64_decode($cat));
        if($img != false){

        imagejpeg($img, "htdocs/".$catt."");
        }
        else{
        echo falsss;
}

$qu="INSERT INTO q(id,q)VALUES(5,'$catt')";
$res=mysql_query($qu);
if($res){
echo "success";
}
else{
echo mysql_error();
?>

Problem: Image is not uploading on the database. Can any one explain?

  • 1
    just how big are these base64 "images"? URLs are length limited, and you risk having your image chopped off/corrupted... and `echo falsss`? what is this `falsss`? – Marc B Jun 16 '15 at 14:22
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 16 '15 at 14:28
  • Yes, I will do it with prepared statements @JayBlanchard Thank you for your advice. – Bhupesh Sharma Jun 17 '15 at 05:04

1 Answers1

1

Base64 encoding needs to be escaped to be part of a URL. Base64 used the problematic characters: "+", "=" and "/".

There are also length limits, from RFC7230:

Various ad hoc limitations on request-line length are found in practice. It is RECOMMENDED that all HTTP senders and recipients support, at a minimum, request-line lengths of 8000

Realistically 2000 character is a good maximum.

URLs are not designed to transmit data, use POST or PUT.

zaph
  • 111,848
  • 21
  • 189
  • 228