Skip to content

Instantly share code, notes, and snippets.

@599316527
Created January 3, 2018 04:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 599316527/9d161467d470fc8863b894da2f80a909 to your computer and use it in GitHub Desktop.
Save 599316527/9d161467d470fc8863b894da2f80a909 to your computer and use it in GitHub Desktop.
PHP Single File Upload
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="." method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
// Modified from https://gist.github.com/taterbase/2688850#file-upload-php
if(!empty($_FILES['uploaded_file']))
{
$path = "/tmp/";
$basename = $_FILES['uploaded_file']['name'];
list($filename, $extension) = explode('.', $basename);
$ts = str_replace('.', '', strval(microtime(true)));
$path .= "{$filename}_{$ts}.{$extension}";
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file {$basename} has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment