Last active
March 22, 2016 14:56
-
-
Save Nyr/98195d1aef734abf629b to your computer and use it in GitHub Desktop.
Very simple file upload script for private use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 '¯\_(ツ)_/¯'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment