Originally posted by Sharif
Ok here is the idea:
User is to upload picture. Max size 500k. Max dimensions: 300x200. Types allowed: .jpg, .gif, .png ONLY. I also need to save teh pictures location in a database table.... WHERE DO I START? I've never done this before so it's time to learn... what now?
You basically need a form like so
<html><title>Upload</title>
<?
$con = mysql_connect("localhost","etc","etc");
mysql_select_db("db",$con);
if ($_POST['Submit']){
$temp_name = $HTTP_POST_FILES['mypic']['tmp_name'];
$size = $HTTP_POST_FILES['myfile']['size'];
$type = $HTTP_POST_FILES['myfile']['type'];
if ($size == 0 || $size > 512000){
echo "The file does not exist or is too big.";
} elseif ($type != "image/gif" && $type != "image/jpeg" && $type != "image/png") {
echo "This type of file is not supported.";
} else {
$picinf = getimagesize($temp_name);
$width = $picinf[0];
$height = $picinf[1];
if ($width > 300 || $height > 200){
echo "The maximum image dimension is 300x200.";
} else {
$uploadpath = "images/"; //Dont forget to chmod it.
switch ( $picinf[2] ) {
case 0:
echo "Couldnt upload: Unknown file type";
break;
case 1:
$dest = $uploadpath.uniqid('img').'.gif';
break;
case 2:
$dest = $uploadpath.uniqid('img').'.jpg';
break;
case 3:
$dest = $uploadpath.uniqid('img').'.png';
break;
}
if ( $dest != '' ) {
if ( move_uploaded_file( $source, $dest ) ) {
echo 'File successfully stored.<BR>';
mysql_query("INSERT INTO files (yadda,yadda) VALUES (width height etc)");
} else {
echo 'File could not be stored.<BR>';
}
}
} else {
?>
<FORM ACTION="thispage.php" METHOD=POST
ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="mypic" SIZE=30>
<INPUT TYPE="submit" NAME="Upload File">
</FORM>
</html>
<?
}