PHP上传文件

时间:2017-03-29 15:53:33

标签: php php-7

我尝试使用php脚本将文件上传到服务器。我将MAMPPHP7.0.8

一起使用

我有HTML表单:

<form enctype="multipart/form-data" action="hi" method="POST">
  <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
    Send File: <input name="userfile" type="file" />
  <input type="submit" value="Send File" />
</form>

简单的php:

  <?php

$uploaddir = 'img/';
print_r($uploadfile = $uploaddir.basename($_FILES['userfile']['name']));

  echo '<pre>';
  if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
      echo "File is valid, and was successfully uploaded.\n";
  } else {
      echo "Possible file upload attack!\n";
  }

  echo 'info:';
  print_r($_FILES);
  echo "<br />";
  print_r($_FILES['userfile']['error']);
  print "</pre>";

?>

我的结果是:

可能的文件上传攻击!

info:Array
(
    [userfile] => Array
        (
            [name] => high-school-icon.png
            [type] => image/png
            [tmp_name] => /Applications/MAMP/tmp/php/phpoaPmYv
            [error] => 0
            [size] => 1807
        )

)

2 个答案:

答案 0 :(得分:1)

Form action documentation

错误: action="hi"

将其更改为: action="simple.php"

<form enctype="multipart/form-data" action="simple.php" method="POST">
  <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
    Send File: <input name="userfile" type="file" />
  <input type="submit" value="Send File" />
</form>

答案 1 :(得分:-2)

抱歉,这是个愚蠢的错误。 $uploaddir = 'img/';

的错误方式