note 90023 added to reserved.variables.files
| From: | camilo3rd at gmail dot com | Date: | Thu, 02 Apr 2009 08:02:38 +0000 |
| Subject: | note 90023 added to reserved.variables.files | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152739@lists.php.net to get a copy of this message | ||
Filename: uploadForm.html
------------------------
<style>
body, table, html
{
font-family: verdana;
font-size: 11px;
}
</style>
<form method="POST" action="uploadFile.php"
enctype="MULTIPART/FORM-DATA">
<table border="0" cellpadding="4" cellspacing="0"
width="400">
<tr>
<td colspan="2" height="10"></td>
</tr>
<tr>
<td colspan="2" height="10"><h2>camilord Sample Upload
File</h2></td>
</tr>
<tr>
<td align="right">Caption:</td>
<td align="left"><input type="text"
name="caption" id="caption" /></td>
</tr>
<tr>
<td align="right" valign="top"><div
style="padding-top: 3px;">Picture:</div></td>
<td align="left">
<input type="hidden" name="MAX_FILE_SIZE"
value="500000000" />
<input type="file" name="picture" id="picture"
/><br />
<font style="font-size: 10px; color: #666666;">Note: Upload max file
size is 2MB.</font>
</td>
</tr>
<tr>
<td align="right"> </td>
<td align="left"><input type="submit"
value="Upload" name="upbtn" id="upbtn" /></td>
</tr>
</table>
</form>
Filename: upload.php
------------------------
<?php
function getFileType($fname)
{
$check_filetype = explode(".", $fname);
$loc_dot = count($check_filetype);
$loc_dot -= 1;
return $check_filetype[$loc_dot];
}
if (isset($_POST["upbtn"]))
{
// get variables from upload form
$uploadfilebasename = stripslashes(trim(basename($_FILES["picture"]["name"])));
$caption = stripslashes(trim(ucwords(strtolower($_POST["caption"]))));
// set upload destination
$uploaddir = "uploads/";
// input validations
if (strlen($uploadfilebasename) < 4)
{
echo '<script>alert("Please browse a
file!");history.go(-1);</script>';
}
else if (strlen($caption) < 3)
{
echo '<script>alert("Please enter your
caption!");history.go(-1);</script>';
}
else
{
// ============== core upload process... [start] =======================
// where the temporary uploadeded file stored
$uploadfilename = $uploaddir . $uploadfilebasename;
// allowed file types
$validMIME =
array('jpg','jpeg','bmp','png','gif');
// act as boolean if the uploaded file is allowed
$isMIME = 'NO';
//get file extension name (sample: .php .doc & etc)
$fType = getFileType($uploadfilebasename);
/*
new name for the file to be uploaded
purpose: so that no duplicate or overwritten files will occur
*/
$newfile = md5(mktime()).'_'.rand(1000,9999).'.'.$fType;
// validating file type
for ($i = 0; $i < count($validMIME); $i++)
{
if (strtolower($fType) == $validMIME[$i])
{
$isMIME = 'YES';
break;
}
}
if ($isMIME == 'YES')
{
//start upload file to temporary storage
move_uploaded_file($_FILES["picture"]["tmp_name"], $uploadfilename);
// rename the file for final filename
rename($uploadfilename,$uploaddir.$newfile);
// check if file is uploaded
if (file_exists($uploaddir.$newfile))
{
echo '<script>alert("Upload Successful!");</script>';
echo '<body>
<div align="center">
<img src="'.$uploaddir.$newfile.'" border="0" /><br
/>
<b>Caption: '.$caption.'</b>
</div>
</body>';
}
else
{
echo '<script>alert("Unable to upload your file! Might exceed to file size
limit.");history.go(-1);</script>';
}
}
else
{
echo '<script>alert("File type not
acceptable!");history.go(-1);</script>';
}
// ============== core upload process... [end] =======================
}
}
else
{
echo '<h1>Direct Access not allowed!</h1>';
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 120.28.222.208
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.files.php
Edit -- https://master.php.net/note/edit/90023
Del: integrated -- https://master.php.net/note/delete/90023/integrated
Del: useless -- https://master.php.net/note/delete/90023/useless
Del: bad code -- https://master.php.net/note/delete/90023/bad+code
Del: spam -- https://master.php.net/note/delete/90023/spam
Del: non-english -- https://master.php.net/note/delete/90023/non-english
Del: in docs -- https://master.php.net/note/delete/90023/in+docs
Del: other reasons-- https://master.php.net/note/delete/90023
Reject -- https://master.php.net/note/reject/90023
Search -- https://master.php.net/manage/user-notes.php