Page 1 of 1

ARG! How do you pass parameters AND a file in HTTP POST?

Posted: Fri Nov 19, 2010 4:18 pm
by Stokestack
Hi all.

This seems to be an oft-asked but never clearly answered question on the Web. I'm sending files to a PHP upload page using HTTP post from C++, and I also want to send the name of the directory I want the file stored in. I don't know where to put that directory-name string in the POST message, nor do I know how to retrieve it using PHP at the receiving end. The upload is working fine, but I admit I don't understand the syntax of the PHP page that's doing the receiving.

The POST request is pieced together line by line, as I found done in various examples. Like this:

Code: Select all

	[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%s\"\r\n",
					   filename.c_str()] dataUsingEncoding:NSUTF8StringEncoding]];
One post suggested that you could add another Content-Disposition line to create another form field for an additional parameter, but this broke the form as far as PHP was concerned.

Primarily I just need to get this working, but I also don't understand where all of the $_FILES info comes from at the PHP receiving end. For example, the POST snippet above clearly contains a value called "filename", but there is no reference to "filename" anywhere in the PHP upload page. Yet it works.

Any insight would be much appreciated!

Re: ARG! How do you pass parameters AND a file in HTTP POST

Posted: Fri Nov 19, 2010 5:05 pm
by pickle
Look at the PHP page for handling HTTP file uploads: http://www.php.net/manual/en/features.f ... method.php . That might give you a bit of an idea how PHP handles file uploads.

What is the purpose of sending the directory? Are you wanting the receiving PHP page to move your uploaded file to that directory?

Re: ARG! How do you pass parameters AND a file in HTTP POST

Posted: Fri Nov 19, 2010 5:30 pm
by Stokestack
Are you wanting the receiving PHP page to move your uploaded file to that directory?
Exactly. I know what happens currently, and I have hard-coded the PHP page to put the uploaded file in a particular place. But some uploads from my app need to go to different places.

Re: ARG! How do you pass parameters AND a file in HTTP POST

Posted: Fri Nov 19, 2010 6:47 pm
by Bind
simply pull the destination directory data value out of the $_POST array and append to the destination directory of the path to the uploads destination in the move_uploaded_file function to mke for dynamic saves in different locations. You can code in checks for existing directories and add directory creation if they do not exist if you want that level of functionality. If you want the true directory names hidden from visitors you can use named aliases with arrayed display_name=>directory pairs to substitute.

example:

Code: Select all

if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$path.'/'.$_POST['dir'].'/'))
   {
      die('unable to move uploaded file to the '.$path.'/'.$_POST['dir'].'/ directory');
   }
you will need to edit permissions on the directories created in order to save to them else you may get permissions errors.

Posted: Fri Nov 19, 2010 10:38 pm
by Stokestack
simply pull the destination directory data value out of the $_POST array
Thanks. The question is, how does it get into that array? Can you include parameters in the URL just as you would with a GET?

UPDATE: Nope. Parameters in the URL aren't parsed for a POST. It looks like you need form fields, and the syntax to create them programmatically is not widely publicized.

Re: ARG! How do you pass parameters AND a file in HTTP POST

Posted: Sat Nov 20, 2010 1:29 am
by s992
$_REQUEST is an array that stores POST and GET.

Re: ARG! How do you pass parameters AND a file in HTTP POST

Posted: Sun Nov 21, 2010 12:09 am
by Bind
How data gets into the $_POST array is by the web site visitor submitting the form. PHP populates the $_POST and $_GET arrays automatically based on the METHOD declaration contained in the html form tag, and $_REQUEST array is automatically populated regardless of the method declaration in the html form tag.

you indeed can create them prgramatically - check out google or http://phpclasses.org/ which has quite a few html form generation classes.

Re: ARG! How do you pass parameters AND a file in HTTP POST

Posted: Sun Nov 21, 2010 11:45 pm
by Stokestack
Bind wrote:How data gets into the $_POST array is by the web site visitor submitting the form.
Thanks for responding, but please note from my original post:
I'm sending files to a PHP upload page using HTTP post from C++
So there is no Web site, and no form to submit. The HTTP POST message is being built in C++ as strings, line by line. Thanks for the reference to that code repository, though. I'm sure I'll find it useful for server-side code.
s992 wrote:$_REQUEST is an array that stores POST and GET.
Thanks. I'll take a look at that.