Hello fellow Daniz, i working on a website and am trying to upload images but i keep getting an error stating that i have undefined functions in my code, could someone point me in the right direction here.
My code is below.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload your pic to our site!</title>
<style type="text/css">
<!--
td {
vertical-align: top;
}-->
</style>
</head>
<body>
<form action="check_image.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td> Your Username </td>
<td><input type="text" name="username" /></td>
</tr>
<td> Upload Image*</td>
<td><input type="file" name="uploadfile" /></td>
</tr>
<tr>
<td colspan="2">
<small><em> * Acceptable image formats include: GIF, JPG/JPEG and PNG.</em></small>
</td>
</tr>
<tr>
<td> Image Caption <br/>
</td>
<td><input type="text" name="caption" /></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<input type="submit" name="submit" value="Upload"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
//setup names of database and table to use
$db_name = "web";
$table_name = "member";
//connect to server and select database
$connection = @mysql_connect("localhost", "root", "")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or
die(mysql_error());
//change this path to match your images directory
$dir ='C:/wamp/www/crap/images';
//make sure the uploaded file transfer was successful
if ($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
{
switch ($_FILES['uploadfile']['error'])
{
case UPLOAD_ERR_INI_SIZE:
die('The uploaded file exceeds the upload_max_filesize directive in php.ini.');
break;
case UPLOAD_ERR_FORM_SIZE:
die('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.');
break;
case UPLOAD_ERR_PARTIAL:
die('The uploaded file was only partially uploaded.');
break;
case UPLOAD_ERR_NO_FILE:
die('No file was uploaded.');
break;
case UPLOAD_ERR_NO_TMP_DIR:
die('The server is missing a temporary folder.');
break;
case UPLOAD_ERR_CANT_WRITE:
die('The server failed to write the uploaded file to disk.');
break;
case UPLOAD_ERR_EXTENSION:
die('File upload stopped by extension.');
break;
}
}
//get info about the image being uploaded
$image_caption = $_POST['caption'];
$image_username = $_POST['username'];
$image_date = date('Y-m-d');
list($width, $height, $type, $attr) = getimagesize($_FILES['uploadfile']['tmp_name']);
// make sure the uploaded file is really a supported image
switch ($type)
{
case IMAGETYPE_GIF:
$image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or
die('The file you uploaded was not a supported filetype.');
$ext = '.gif';
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or
die('The file you uploaded was not a supported filetype.');
$ext = '.jpg';
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or
die('The file you uploaded was not a supported filetype.');
$ext = '.png';
break;
default:
die('The file you uploaded was not a supported filetype.');
}
//insert information into image table
$query = 'INSERT INTO images (image_caption, image_username, image_date)
VALUES
("'.$image_caption.'","'.$image_username.'","'.$image_date.'")';
$result = mysql_query($query, $db) or die (mysql_error($db));
//retrieve the image_id that MySQL generated automatically when we inserted
//the new record
$last_id = mysql_insert_id();
//because the id is unique, we can use it as the image name as well to make
//sure we don’t overwrite another image that already exists
$imagename = $last_id .$ext;
// update the image table now that the final filename is known.
$query = 'UPDATE images SET image_filename = "'.$imagename.'" WHERE image_id = '.$last_id;
$result = mysql_query($query, $db) or die (mysql_error($db));
//save the image to its final destination
switch ($type)
{
case IMAGETYPE_GIF:
imagegif($image, $dir .'/'. $imagename);
break;
case IMAGETYPE_JPEG:
imagejpeg($image, $dir .'/'. $imagename, 100);
break;
case IMAGETYPE_PNG:
imagepng($image, $dir .'/'. $imagename);
break;
}
imagedestroy($image);
?>
<html>
<head>
<title> Here is your pic!</title>
</head>
<body>
<h1> So how does it feel to be famous? </h1>
<p> Here is the picture you just uploaded to our servers: </p>
<img src="images/<?php echo $imagename; ?>" style="float:left;">
<table>
<tr><td>Image Saved as: </td> <td > <?php echo $imagename; ?></td></tr>
<tr><td>Image Type: </td> <td> <?php echo $ext; ?></td></tr>
<tr><td>Height: </td> <td> <?php echo $height; ?></td></tr>
<tr><td>Width: </td> <td> <?php echo $width; ?></td></tr>
<tr><td>Upload Date: </td><td> <?php echo $image_date; ?></td></tr>
</table>
</body>
</html>