Can anyone tell me what I am missing or not doing in this script. I am having problems with file upload. In this instance I am trying to upload an image to a file and storing the filename in the database.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guitar Wars - Add Your High Score</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Guitar Wars - Add Your High Score</h2>
<?php
//Define the upload path and maximum file size constants
define('GW_UPLOADPATH', 'userimages/');
define('GW_MAXFILESIZE', 32768);
if (isset($_POST['submit'])) {
// Grab the score data from the POST
$name = $_POST['name'];
$score = $_POST['score'];
//$screenshot =$_POST['screenshot'];
$screenshot = $_FILES['screenshot']['name'];
$screenshot_type = $_FILES['screenshot']['type'];
$screenshot_size = $_FILES['screenshot']['size'];
if (!empty($name) && !empty($score) && !empty($screenshot)) {
if ((($screenshot_type == 'image/gif') || ($screenshot_type == 'image/jpeg') || ($screenshot_type == 'image/pjpeg') || ($screenshot_type == 'image/png'))
&& ($screenshot_size > 0) && ($screenshot_size <= GW_MAXFILESIZE)) {
if ($_FILES['screenshot']['error'] == 0) {
echo $_FILES['screenshot']['error'];
echo $screenshot_type;
echo $screenshot_size;
echo $screenshot;
echo $_FILES['screenshot']['tmp_name'];
//Move the file to the target upload folder
$target=GW_UPLOADPATH . $screenshot;
if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)){
// Connect to the database
$dbc = mysql_connect('localhost', '***', '***');
mysql_select_db('****', $dbc) or die('could not connect' .mysql_errno() .mysql_error());
echo 'we\'ve connected';
// Write the data to the database
$query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score', '$screenshot')";
mysql_query($query, $dbc) or die('could not query' .mysql_errno() .mysql_error());
// Confirm success with the user
echo '<p>Thanks for adding your new high score!</p>';
echo '<p><strong>Name:</strong> ' . $name . '<br />';
echo '<strong>Score:</strong> ' . $score . '</p>';
echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
echo '<p><a href="index.php"><< Back to high scores</a></p>';
// Clear the score data to clear the form
$name = "";
$score = "";
$screenshot ="";
mysql_close($dbc);
}
else {
echo '<p class="error">Sorry, there was a problem uploading your screen shot image</p>';
}
}
}
else {
echo '<p class="error">The screen shot must be a GIF, JPEG, or PNG image file no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.</p>';
}
// Try to delete the temporary screen shot image file
@unlink($_FILES['screenshot']['tmp_name']);
}
else {
echo '<p class="error">Please enter all of the information to add your high score.</p>';
}
}
?>
<hr />
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo GW_MAXFILESIZE; ?>" />
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br />
<label for="score">Score:</label>
<input type="text" id="score" name="score" value="<?php if (!empty($score)) echo $score; ?>" />
<br />
<label for="screenshot">Screenshot:</label>
<input type="file" id="screenshot" name="screenshot" />
<hr />
<input type="submit" value="Add" name="submit" />
</form>
</body>
</html>
It dumps down into the error script below:
echo '<p class="error">Sorry, there was a problem uploading your screen shot image</p>';
I am able to echo all of the data associated with the file, such as filename, size and type, but it just jumps into the above exception. The error for the file upload is 0. So I am not sure why the file is not being uploaded.