What i need it to do also is insert the width and height of the image that was uploaded to my database. When i click Upload it does everything fine but for some reason for every image it inserts width and height as 127 and i don’t know why. Could someone please help me, here is the code.
<?php
// Settings
define('MAX_FILE_SIZE', 8388608); // 8 MB
define('UPLOAD_PATH', 'images/');
$allowed_types = array('image/gif', 'image/pjpeg', 'image/jpeg');
// Upload handling
if (isset($_POST['submit'])) {
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_NO_FILE:
$error = 'You did not upload a file!';
break;
case UPLOAD_ERR_OK:
if ($_FILES['file']['size'] > MAX_FILE_SIZE) {
$error = 'File is too large!';
break;
}
if (!in_array($_FILES['file']['type'], $allowed_types)) {
$error = 'This file types is not allowed!';
break;
}
if (!move_uploaded_file($_FILES['file']['tmp_name'], UPLOAD_PATH . $_FILES['file']['name'])) {
$error = 'Could not move file, upload failed!';
break;
}
list($width, $height) = getimagesize('images/'.$_FILES['file']['name']);
echo "Height = $height & Width = $width";
// Add file entry
mysql_connect('localhost', 'root', 'xxxx') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());
$sql = "INSERT INTO babygallery (Image, caption, height, width) VALUES ('".$_FILES['file']['name']."', '" . ($caption) . "', '" . ($height) . "', '" . ($width) . "' )";
mysql_query($sql) or die(mysql_error());
break;
}
$message = isset($error) ? '<span style="color:red">'.$error.'</span>'
: '<span style="font-weight:bold">Upload successfull</span>';
}
?>
<html>
<head>
<title>Upload file</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<?php if (isset($message)) echo $message; ?>
<br><br>
Choose a file to upload:<br>
<input type="file" name="file"><br>
<br>
<textarea name="caption" cols="50"><?php echo $caption ?></textarea>
<br>
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>