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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Stokestack
Forum Newbie
Posts: 14
Joined: Thu Sep 16, 2010 2:37 am

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

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Stokestack
Forum Newbie
Posts: 14
Joined: Thu Sep 16, 2010 2:37 am

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

Post 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.
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

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

Post 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.
Stokestack
Forum Newbie
Posts: 14
Joined: Thu Sep 16, 2010 2:37 am

Post 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.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

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

Post by s992 »

$_REQUEST is an array that stores POST and GET.
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

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

Post 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.
Stokestack
Forum Newbie
Posts: 14
Joined: Thu Sep 16, 2010 2:37 am

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

Post 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.
Post Reply