Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7732413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:43:28+00:00 2026-06-01T06:43:28+00:00

I don’t know if the problem is in my PHP code or on the

  • 0

I don’t know if the problem is in my PHP code or on the objective-c side. I don’t understand this very well so would appreciate any help getting files to upload from the iPad app I am creating to a mysql database. Right now, only the description of the file is getting posted into the database, but the blob/file is not. Any help would be much appreciated!

Here is my PHP code:

<?php
$username = "person";
$password = "xxxxxxx";
$database = "database";

mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");

$file = $_FILES['file'];
$name = $file['tmp_name'];
$testpage = file_get_contents($name);
$testpage = mysql_real_escape_string($testpage); 

mysql_query("INSERT INTO tbldocs(Title,Document) VALUES('some title','$testpage')");             
mysql_close();
?>

Here is my objective-c code, packaging it into an HTTP packet

NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[post_dict setObject:@"test_value" forKey:@"test_key"];
[post_dict setObject:[NSURL fileURLWithPath:[pdfUrl absoluteString]] forKey:@"file"];
NSData* regData = [self generateFormData:post_dict];
[post_dict release];

NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://myserver/upload.php"]];
[post addValue: @"multipart/form-data; boundary=_insert_some_boundary_here_" forHTTPHeaderField: @"Content-Type"];
[post setHTTPMethod: @"POST"];
[post setHTTPBody:regData];
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:post returningResponse:&response error:&error];
NSLog(@"%@", [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);

Thanks,
Rossi

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:43:30+00:00Added an answer on June 1, 2026 at 6:43 am

    The variable $file['name'] is the name of the file as posted in the upload (e.g. the filename from the local system).

    The variable $file['tmp_name'] is the name of the file on the server. This is the name of the file that contains the content you want to put into the database.

    That should address the php side. The Objective-C code is a little more complicated. You need to manually construct the mime-encoded content that is being used in the upload. There is a simple example on CocoaDev in the generateFormData method, in the line example:

    [post_dict setObject:[NSURL fileURLWithPath:@"/Butterfly.tif"] forKey:@"file1"];
    

    he is setting the PHP equivalent of the $_FILE['file1'] variable here, in your case, you probably want to choose forKey:@"file" instead.

    Edit Self-contained synchronous variant. I have the following parameters: DestUrl – an NSURL containing the destination, fileName – an NSString containing the name of the file and finally FileData, an NSData containing the content of the file.

    NSMutableData *data = [[NSMutableData alloc] initWithCapacity:100];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:DestUrl];
    [request setHTTPMethod:@"POST"];
    // Define the boundary
    NSString *boundary = [NSString stringWithFormat:@"weasel_grapple_%ld_foo", (long)time(NULL)];
    // Tell MIME that content type
    [request    addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]
      forHTTPHeaderField:@"Content-Type"];
    // First piece of data
    [data appendData:[[NSString stringWithFormat:@"--%@\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    // Add in the form field name (name), filename
    [data appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n",
                         @"file", fileName] dataUsingEncoding:NSASCIIStringEncoding]];
    // And it's binary data here
    [data appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\n\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    // This is content of the file
    [data appendData:FileData];
    // Need a blank link as a separator of form-data items
    [data appendData:[[NSString stringWithString:@"\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    // Mark the end of the upload message
    [data appendData:[[NSString stringWithFormat:@"--%@--\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    
    [request setHTTPBody:data];
    
    NSURLResponse *response;
    NSError *error;
    NSData *retData = [NSURLConnection sendSynchronousRequest:request
                                                returningResponse:&response
                                                     error:&error];
    

    If you want to take a file URL, and turn it into FileData then do:
    NSData *FileData = [NSData dataWithContentsOfFile:fileURL];

    On the php side, test first with a simple upload form, making sure that uploaded data is getting into the database and work from there.

    My snippet of file upload to the database on the php side looks like. It’s pretty horrific, but it gets the upload across the line.:

    foreach ($_FILES as $key => $value) {
        $file_tag = $key;
        $fname = basename($value['name']);
        $name = $value['tmp_name'];
    
        $testpage = file_get_contents($name);
    $testpage = addslashes($testpage);
    
        mysql_query("INSERT INTO tbldocs(Title,Document) VALUES('some title','$testpage')");
    }
    

    Note also: If your upload file is larger than 64K then you must specify Document as a field type of MEDIUMBLOB. The regular BLOB data type supports up to 64K data only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this is an eclipse specific problem but whenever I declare a
Don't be scared of the extensive code. The problem is general. I just provided
Don't know if this has been asked before, so point me to another question
Don't think that I'm mad, I understand how php works! That being said. I
Don't know if anyone can help me with this or if it's even possible.
Don't be frightened, its a very basic code. Just wanted to check with you
I don't know: if this works. if it's a good idea. what it is
I don't know if this question is trivial or not. But after a couple
Don't overthink this - there's a very commonly used term and I ... have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.