2

I have a basic username & password form which also allows you to upload an image with it. There's a create button, which takes the user to uploader.php which both uploads the image and inputs the username & password into the database.

Within the form tag:

< form enctype="multipart/form-data" method="POST" action="uploader.php?uploader=avatar&username=< ?php echo $_POST['username']; ?>" >

The problem:

The username won't post, nor any other posts for that matter. All fields are inside the form. I have checked PHP file upload form cannot submit a POST variable? and within php.ini post_max_size = 8M, and upload_max_filesize = 2M

5
  • Its in the url not in the post data Commented Aug 21, 2012 at 3:05
  • username is in the url Commented Aug 21, 2012 at 3:10
  • That's the name of the username input field Commented Aug 21, 2012 at 3:11
  • 1
    Are you trying to submit a username and password at the same time as uploading a file? Commented Aug 21, 2012 at 3:20
  • Well, they are supposed to get posted to uploader.php, then if the upload is successful they should be input into the database. But yes, to answer your question Commented Aug 21, 2012 at 3:25

4 Answers 4

4

Use <input type="hidden"/> to post username and other info.

<form enctype="multipart/form-data" method="POST" action="uploader.php">
    <input type="hidden" name="uploader" value="avatar"/>
    <input type="hidden" name="username" value="<?php echo $_POST['username']; ?>" />
    ...
</form>

Sample.php

<form enctype="multipart/form-data" method="POST" action="uploader.php">
  <br/>Username : <input type="text" name="username"/>
  <br/>Password : <input type="password" name="password"/>
    <input type="hidden" name="uploader" value="avatar"/>
   <br/>File : <input type="file" name="file"/>
   <br/><input type="submit"/>
</form>

uploader.php

<?php
  print_r($_POST)  // debug  $_POST
  print_r($_FILES) // file

  //OR
  echo $_POST["username"];
  $file=$_FILES["file"];
  print_r(file);
?>
Sign up to request clarification or add additional context in comments.

6 Comments

But the password field input type needs to be "password" (this also needs to be posted I should have mentioned)
It gets encypted and posted to the uploader.php file, which then inserts it into the database (I'm guessing this is an insecure approach)
What do you mean by embed it with the hidden field? I'm not sure how else the password can get transferred to the uploader.php file without posting it in the URL
Also - with making the input type hidden, this subsequently hides the field for the user to insert the username
Sorry! what I understood from your post is that you wanted to pass username and uploader values (which are already available) to the request but if you wish to take input from the user then choose <input type='text or password'/>.
|
1

It sounds like you want to submit the username and password and upload a file all in the one submit.

If this is what you want, you need something like the following:

<form enctype="multipart/form-data" method="POST" action="uploader.php">
<input type="text" name="username" value="" />
<input type="password" name="password" value="" />
<input type="file" name="uploaded" />
...
</form> 

The username and password will be available in $_POST[] and the file will be present in $_FILES[].

6 Comments

This still doesn't "POST" the username, strangely.
So what do print_r($_POST);, print_r($_GET); and print_r($_REQUEST); show? The output of those should go a long way toward helping you figure what's missing.
Oh okay, it works as I used POST on the uploader.php... but I'm confused, I thought I should have to post the username on the original PHP page and then have to GET it on uploader.php?
The superglobal $_POST[] is the contents of all variables sent to the server in a HTTP POST request. You specify whether a form makes a GET request or a POST request by setting the method parameter to the <form> element. (But remember you can only upload a file in a POST.)
I had this problem when the files I was attempting to upload were larger than the max filesize PHP was accepting. Look at: ini_get('post_max_size') and ini_get('upload_max_filesize') to see if your file is too big.
|
1

I had this problem when the files I was attempting to upload were larger than the max filesize PHP was accepting. Look at:

ini_get('post_max_size')

and

ini_get('upload_max_filesize')

to see if your file is too big. My solution was to use empty($_POST) to determine if the file was too big (or some other posting problem occurred) and throw an Exception.

1 Comment

Not a solution to the question.
-1

Oddly I had the same issue, until I add the enctype="multipart/form-data" attribute.. After that, all worked

Comments

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.