Applying File size and extension
Posted: Mon Jul 20, 2009 3:49 am
Hi
I'm new to the world of PHP but anyway
I ahve this script to upload a file to the server.
Pretty simple
What it does is upload the file and assigns a random number to the file
Now I want it to limit the file size and type of file
If it exceeds size or is a denied file extensions (ex. php) it displays a error
I added it to my script, so far it worked when I uploaded a php file
but it looked like this:
this file is not allowed.the file uploaded successful
So it displays a error, but it still uploaded the file
I tried doing this many a ways
I cant seem to get it to work
either it displays a error and does it for all files
or displays a error and still uploads
here is my upload script:
Is this solvable?
I dont want PHP files to be uploaded and max file size is 2MB but ever other file is allowed.
Thanks
I'm new to the world of PHP but anyway
I ahve this script to upload a file to the server.
Pretty simple
What it does is upload the file and assigns a random number to the file
Now I want it to limit the file size and type of file
If it exceeds size or is a denied file extensions (ex. php) it displays a error
Code: Select all
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
but it looked like this:
this file is not allowed.the file uploaded successful
So it displays a error, but it still uploaded the file
I tried doing this many a ways
I cant seem to get it to work
either it displays a error and does it for all files
or displays a error and still uploads
here is my upload script:
Code: Select all
<?php
echo "<html><head><title>Uploads For ALL - Uploaded File</title></head><body bgcolor=\"black\" text=\"white\" vlink=\"white\">";
echo "<center>";
if (file_exists("favicon.ico")) {
echo "<link REL=\"shortcut icon\" HREF=\"favicon.ico\" TYPE=\"image/x-icon\">\n";
}
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
$ok = 1;
$ok = 0;
}
$ext = findexts ($_FILES['uploaded']['name']) ;
$ran = rand () ;
$ran2 = $ran.".";
$target = "images/";
$target = $target . $ran2.$ext;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file has been uploaded as ".$ran2.$ext;
echo "<br>The file is located here: http://www.pspdd.comze.com/upload/images/".$ran2.$ext;
echo "<br>Click <a href=\"http://www.pspdd.comze.com/upload/images/$ran2$ext\">Here</a> to view/download the file.";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
echo "<form><input type=\"button\" value=\"Back to Home\" onClick=\"javascript:history.go(-1)\"></form>";
echo "<form method=\"post\"><input type=\"button\" value=\"Close Page\" onclick=\"window.close()\"></form>";
echo "</center>";
?>
I dont want PHP files to be uploaded and max file size is 2MB but ever other file is allowed.
Thanks