PHP Upload File

╄→尐↘猪︶ㄣ 提交于 2021-01-27 14:02:01

问题


I'm trying to upload file to server with php script. I'm Using MAMP with PHP7.0.8

I have HTML form:

<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>

And simple 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>";

?>

My result is:

Possible file upload attack!

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

)

回答1:


Form action documentation

Error: action="hi"

Change it to: 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>



回答2:


Sorry, it was stupid mistake. Wrong way for $uploaddir = 'img/';



来源:https://stackoverflow.com/questions/43098442/php-upload-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!