简体   繁体   English

用PHP上传文件时遇到问题

[英]Trouble with file uploads in PHP

Here I have a basic html form with an choose file input and submit button. 在这里,我有一个带有选择文件输入和提交按钮的基本html表单。 In my linked PHP page, I am trying to display the information of the file inside of an array in the browser, but am getting an undefined index error Undefined index: file on each of the two lines of code where my two variables are. 在链接的PHP页面中,我试图在浏览器中的数组内部显示文件的信息,但是在我的两个变量所在的两行代码中的每行上均出现Undefined index: file错误。 Why am I getting this error? 为什么会出现此错误?

My HTML 我的HTML

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <form action="upload.php" method="POST" enctype="mulitpart/form-data">
      <input type="file" name="file">
      <button type="submit" name="submit">UPLOAD</button>
    </form>
  </body>
</html>

My PHP code 我的PHP代码

  <?php

  if (isset($_POST['submit'])) {
    $file = $_FILES['file'];
    print_r($file);
    $fileName = $_FILES['file']['name'];
  }

您拼写为“ multipart / form-data错误”

your enctype="mulitpart/form-data" must enctype="multipart/form-data" 您的enctype =“ mulitpart / form-data”必须enctype =“ multipart / form-data”

and then on your upload.php 然后在您的upload.php上

$nameFile = $_FILES['file']['name']; //for name of picture
    $sizeFile = $_FILES['file']['size']; // for size picture
    $error = $_FILES['file']['error']; // for error 
    $tmpName = $_FILES['file']['tmp_name']; // 

if you want to show it you must convert to array like 如果要显示它,必须将其转换为数组

$data [
$nameFile, $sizeFile, $error, $tmpName
];

and show with print_r($data); 并显示为print_r($ data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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