User Tools

Site Tools


docu:snipper:program:php_upload_file

Run a simple php server to upload files


Simply, put this code snippet in an upload.php file somewhere, and run a simple php server using the cli

upload.php
<?php
 
$target = 'uploads/';
@mkdir($target, 0755);
 
if (isset($_POST['submit'])) {
  $target = 'uploads/'; @mkdir($target, 0755);
  move_uploaded_file( $_FILES['file']['tmp_name'], \
    $target.$_FILES['file']['name'] );
  echo 'The file was uploaded successfully<br><br>';
}
 
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Upload Your File</title>
  </head>
  <body>
    <div id="container">
      <form enctype="multipart/form-data" action="" method="post">
        <label for="file">Choose a file to upload:</label>
        <input id="file" type="file" name="file" /><br/>
        <input type="submit" value="upload file" name="submit" />
      </form>
    </div>
  </body>
</html>

Run the server using php-cli package (php$version-cli)

# install the package (on a Debian based Linux) if needed
apt-get install php7.3-cli --no-install-recommends
 
# run the server on the same path as the upload.php script
php -S 0.0.0.0:8080

Now you can access your uploader simple server on

http://127.0.0.1:8080/upload.php


And woola!!

docu/snipper/program/php_upload_file.txt · Last modified: 2020/08/20 21:18 by admin