Restriktion på upload af billeder
Jeg har et simpelt php-script som kan uploaded en fil til ens server samt smide noget data i en database. Scriptet fungerer fint, og jeg skal udelukkende bruge det til at uploade billeder med. Men der er 2 funktioner jeg savner:- maks størrelse på filen der bliver uploadet (f.eks. 100 kb)
- check om højde/bredde på billedet er korrekt (300 x 200 i dette tilfælde)
Kan man lave det i scriptet?
--------------------------------------
<?php
$allowed = array("jpeg","gif","png","jpg");
?>
<form action="" method="post" enctype="multipart/form-data">
Choose file to upload <input name="upload" type="file">
<input name="sb" id="sb" value="go" type="submit">
</form>
<?php
mysql_connect("localhost","username","password") or die("Could not connect: " . mysql_error());
mysql_select_db("database");
$result = mysql_query("select MAX(id) as maxnum from files");
$row = mysql_fetch_array($result);
$number = $row[maxnum] + 1;
if(isset($_POST['sb'])) {
$pathInfo = pathinfo($_FILES["upload"]["name"]);
$extension = $pathInfo['extension'];
$dir = "uploadFolder";
if(!in_array($extension, $allowed)) die("Extension not allowed!");
if(move_uploaded_file($_FILES['upload']['tmp_name'], "$dir/$number".$_FILES['upload']['name'])) {
print "Your new file can be viewed/download at <input name=\"newfile\" value=\"http://$_SERVER[HTTP_HOST]/test4/$dir/$number".$_FILES['upload']['name']."\" type=\"text\" style=\"width: 300px\">";
}else{
print "File could not be uploaded";
}
mysql_free_result($result);
mysql_query("insert into files(id) values ('$number')");
print mysql_error();
}
?>