简体   繁体   中英

Trouble accessing HTML form data using PHP

I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:

<html>
   <body>
      <form action="fileuploader.php" method="POST" enctype="multipart/form-data">
         <input type="file" name="filename" />
         <input type="submit"/>
      </form>
   </body>
</html>

My php code so far is one line and it doesn't do anything:

<?php
echo $_POST['filename'];
?>

The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?

You can print the filename using the following code:

<?php
echo $_FILES["filename"]["name"];
?>

Based on your code I modified it. Have a try it.

HTML Part

<html>
  <body>
    <form action="fileuploader.php" method="POST" enctype="multipart/form-data">
      <input type="file" name="filename" />
      <input type="submit" name="submit" />
    </form>
 </body>
</html>

PHP

if (isset($_POST['submit'])) {
  // Check if files array is not empty
  if (!empty($_FILES)) {
    $imageName = $_FILES['filename']['name'];
    echo $imageName;
    // Insert your code related to upload 
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM