PHP file upload can't send POST data on same form

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


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

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[] .


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.

链接地址: http://www.djcxy.com/p/74366.html

上一篇: AngularJS不发送隐藏的字段值

下一篇: PHP文件上传无法在同一表单上发送POST数据