Skip to content

Instantly share code, notes, and snippets.

@Nyr
Last active March 22, 2016 14:56
Show Gist options
  • Save Nyr/98195d1aef734abf629b to your computer and use it in GitHub Desktop.
Save Nyr/98195d1aef734abf629b to your computer and use it in GitHub Desktop.
Very simple file upload script for private use
<?php
// Very simple file upload script
// ----------------------------------------------
// I'm a shitty coder, so this is a shitty script
/////////////////////////////////////////////////////////////
// Fill these three variables
$allowedip = '1.2.3.4'; // IP address allowed to upload
$serveurl = 'https://uploads.example.com/';
$uploaddir = '/srv/uploads.example.com/htdocs/';
// No need to edit from here
/////////////////////////////////////////////////////////////
if ($_SERVER['REMOTE_ADDR'] == "$allowedip"){
if (isset($_FILES['userfile'])){
$fullfilename = basename($_FILES['userfile']['name']);
$dirtyfilename = pathinfo($fullfilename) ['filename'];
$cleanfilename = preg_replace("/[^A-Za-z0-9 _.-]/", "", $dirtyfilename);
$nospacesfilename = str_replace(' ', '-', $cleanfilename);
$extension = pathinfo($fullfilename) ['extension'];
$randomstring = substr(str_shuffle(md5(time())) , 0, 10);
$finalfilename = $nospacesfilename . '-' . $randomstring . '.' . $extension;
$finalfilepath = $uploaddir . $finalfilename;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $finalfilepath)){
echo "<a href='$serveurl$finalfilename'>$serveurl$finalfilename</a>";
} else {
echo 'Error';
}
} else {
echo '<form enctype="multipart/form-data" action="" method="POST">
<input name="userfile" type="file" />
<input type="submit" value="Upload" />
</form>';
}
} else {
echo '&#xAF;&#x5C;&#x5F;&#x28;&#x30C4;&#x29;&#x5F;&#x2F;&#xAF;';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment