0

I've a problem: I want to send a picture from my iOS app to a php script to insert this image in a mysql db. The field in mysql db for the image is LONGBLOB. The image that is sending is _photoImageView.image The method I created in Xcode is as follows:

NSData *dataForImage = UIImagePNGRepresentation(_photoImageView.image);


    NSMutableString *strURL = [NSMutableString stringWithFormat:@"http://localhost/myfff/join.php?username=%@&password=%@&photo=%@",_txtUsername.text,_txtPassword.text, dataForImage];
[strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strURL]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

In the php script in the affected part, I do this:

$data = addslashes(fread(fopen($_FILES[photo], "rb"))); $query = "INSERT INTO mytb VALUES (' ','$username','$password'','$data')";

The insertion does not occur .. where am I wrong? Help me please!

Joker
  • 167
  • 1
  • 3
  • 12
  • You need to debug this piece by piece. As a starter for ten, attempting to use `http://localhost` as a destination URL from within iOS isn't going to work, as localhost will in effect be the phone itself. Additionally, encoding an image into a GET request won't work unless the image is very, very, small. – John Parker Jul 10 '13 at 11:21
  • 1
    I make the tests on the iphone simulator, so it works with localhost, in fact if I run the script without the part of the image, the insertion occurs. – Joker Jul 10 '13 at 11:31

1 Answers1

1

If you are getting other posted data on your php script i.e username & password then the issue is that you will not get data in $_FILES array since the Xcode you share is not uploading the data as form (multipart/form-data) instead it's posting the data (i believe), so i think you will be getting raw image data in $_POST['photo'] and query should be:

$data = $_POST['photo'];

$query = "INSERT INTO mytb VALUES (' ','$username','$password'','$data')";

Even if you get the photo in $_FILES then still your using wrong thing to fread it should be:

$data = addslashes(fread(fopen($_FILES['photo']['tmp_name'], "rb")));

Umair Khan
  • 473
  • 2
  • 15
  • 1
    So what? Will you help me make the correct method to send the photos, and maybe even the script in php? please – Joker Jul 10 '13 at 16:58
  • Have you checked `$_POST['photo'];`? Are you getting raw photo data there? – Umair Khan Jul 11 '13 at 06:17
  • 1
    Yes, I set $ _POST ['photo'], but it doesn't insert anything – Joker Jul 11 '13 at 10:05
  • ok, please `print_r($_POST);` and `print_r($_FILES);` to check what these variables contains. If there aren't any photo related data then you need to check your Iphone application code as it's then not sending the photo data. See the Iphone application code to upload the file via PHP here: http://stackoverflow.com/questions/10711481/sending-file-from-ios-to-php-using-post OR http://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post – Umair Khan Jul 14 '13 at 05:30