Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Upload File

Tags:

php

php-7

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
        )

)
like image 368
Rost Avatar asked Mar 30 '26 17:03

Rost


1 Answers

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>
like image 67
Sahil Gulati Avatar answered Apr 02 '26 06:04

Sahil Gulati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!