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!

2
  • 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. Commented Jul 10, 2013 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. Commented Jul 10, 2013 at 11:31

1 Answer 1

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")));

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

4 Comments

So what? Will you help me make the correct method to send the photos, and maybe even the script in php? please
Have you checked $_POST['photo'];? Are you getting raw photo data there?
Yes, I set $ _POST ['photo'], but it doesn't insert anything
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: stackoverflow.com/questions/10711481/… OR stackoverflow.com/questions/8564833/…

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.