Skip to content

Instantly share code, notes, and snippets.

@Zyber17
Created January 24, 2010 21:24
Show Gist options
  • Save Zyber17/285457 to your computer and use it in GitHub Desktop.
Save Zyber17/285457 to your computer and use it in GitHub Desktop.
<?php
/**
* This function will handle all of our file uploading
*/
function uploadFile(){
if(isset($_POST['upload_button'])){
$file = $_FILES['file_upload']; //This is our file variable
$name1 = $file['name'];
$ran = rand(0, 9999);
function findexts ($filename)
{
$filename = strtolower($filename);
$exts = split("[/\\.]", $filename);
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This applies the function to our file
$ext = findexts ($_FILES['file_upload']['name']) ;
$disallowed_extensions = array('php', 'perl', 'exe');
if (in_array($ext, $disallowed_extensions))
{
$ext = 'txt';
echo 'Your file was changed to a txt for safety reasons. <br />';
}
$name2 = $ran.'.'.$ext;
$name = $name2;
$tmp = $file['tmp_name'];
$size = $file['size'];
$directories = $_SERVER['REQUEST_URI'];
$slash = explode('/', $directories);
$fileplace = $slash[count($slash) -1];
$dpath = explode($fileplace, $directories);
$max_size = 5 * 1024 * 1024; //5 megabytes
$upload_dir = 'uploads/';
$path = 'http://' . $_SERVER['HTTP_HOST'] . $dpath[0] . $upload_dir . $name;
$Lines = file("database.txt");
foreach($Lines as $Key => $Val)
{
$Data[$Key] = explode("||", $Val);
}
//////
$filename = 'database.txt';
$somecontent = "\n" . $file['name'] . "||" . date("Y-m-d") . "||" . $name;
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
//////
if(!is_dir($upload_dir))
{
echo $upload_dir . ' is not a directory';
exit();
}
else
{
if($size > $max_size)
{
echo 'The file you are trying to upload is too big.';
}
else
{
$search = glob($upload_dir . $name);
if($search == null)
{
if(!is_uploaded_file($tmp))
{
echo 'Could not upload your file at this time, please try again';
}
else
{
if(!move_uploaded_file($tmp, $upload_dir . $name))
{
echo 'Could not move the uploaded file.';
}
else
{
echo 'File: Uploaded <br /> Location: ' . $path . '<br />' . $Data[0][2];
}
}
}
else
{
echo "Unlucky dude, either your server is full, or the random name wasn't random enough. Try to upload the file again, the random name issue will be addressed in a later version. Thanks!";
}
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="reset.css" />
<link rel="stylesheet" type="text/css" href="index.css" />
<script src="jquery.js" type="text/javascript"></script>
<title>Uploady Rev2 v0.1</title>
</head>
<body>
<?php
uploadFile();
?>
<div id="wrapper">
<div id="upload"> <!--style="display: none;"-->
<form action="index.php" method="post" enctype="multipart/form-data">
<p><label>Choose a file to upload</label><br /><input type="file" name="file_upload" id="file"/><br />
<input type="submit" name="upload_button" value="Upload" /><!--<div id="load"></div>--></p>
</form>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment