Forum Moderators: coopster
move_uploaded_file($_FILES[’Filedata’][’tmp_name’], "./files/".$_FILES[’Filedata’][’name’]);
chmod("./files/".$_FILES[’Filedata’][’name’], 0777);
echo " ";
$one = range('a','z');
$two = range('z','a');
$three = array_merge($one, $two);
shuffle($three);
$random_str = 'Img_'.$three[2].$three[4].$three[6].$three[8].$ext;
1)Create a unique id - couple of arrays then merge/shuffle to make random
[edited by: Readie at 10:35 pm (utc) on Mar 15, 2010]
//Get the filetype eg .swf (including the ".")
$ext = strrchr($_FILES['filedata']['name'], '.');
//accepted tags
$allowed = array(".FLV", ".flv", ".swf", ".SWF");
//Check and flag up error
if (!in_array($ext, $allowed)) {
echo "This filetype is not allowed!";
exit;
}
//Get the filetype eg .swf (including the ".")
$ext = strrchr($_FILES['filedata']['name'], '.');
//accepted tags- note the escaped dot (\.) and the i on the end to make it case insensitive
$allowed = array(
'/^\.flv$/i',
'/^\.swf$/i'
);
$count = count($allowed);
$match = 0;
$i = 0;
//loop through all accepted tags and check them against $ext
//stop looping on a match
while($i < $count && $match != 1) {
if(preg_match($allowed[$i], $ext)) {
$match = 1;
$i = $count;
} else {
$i++;
}
}
//Check and flag up error
if ($match != 1) {
echo "This filetype is not allowed!";
exit;
}
if(preg_match('/^\.jpg$/i', $ext)) {
$match = 1;
} else {
$match = 0;
}
$now = date(YmdHis)
$prev = ($now - (60 * 5))
$sql = 'SELECT * FROM uploads WHERE upload_time <= ' . $prev;
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
for($i = 0; $i < $rows; $i++) {
$file = mysql_result($result, $i, "file_name");
$ftime = mysql_result($result, $i, "upload_time");
unlink('path/to/file/' . $file);
$sql = 'DELETE FROM uploads WHERE file_name = "' . $file . '" AND upload_time = "' . $ftime . '"';
mysql_query($sql);
}
//Get the filetype eg .swf (including the ".")
$ext = strrchr($_FILES['filedata']['name'], '.');
//Check and flag up error
if ($ext != ".jpeg" || ".JPEG" || ".jpg" || ".JPG") {
echo "This filetype is not allowed!";
exit;
}
the filename should be unique to the user currently uploading the image.
When lots of users start using the application and upload their images, the upload directory would fill up quickly. How can I prevent this?