O'Reilly Forums: Chapter 5 (page 269) - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 5 (page 269) Trouble uploading images

#1 User is offline   Big Lew 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 03-January 14

Posted 28 May 2014 - 06:48 PM

I am having trouble with the code on page 269. When I pull up the page in the browser everything starts off great. it allows me to enter the name, score and browse for the image. But when I click submit, I get the error message where it tells me that the file must be a GIF, JPEG or PNG. I am using the exact same file that worked previously and it was no where near the maximum file size. I attached the addscore.php file.

<!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="style1.css" />
</head>
<body>
<h2>Guitar Wars - Add Your High Score</h2>

<body>
<?php
// Define the upload path and maximum file size constants
require_once('appvars.php');

// define the database connection constances
require_once('connectvars.php');

if (isset($_POST['submit'])) {
// Grab the score data from the POST
$name = $_POST['name'];
$score = $_POST['score'];
$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) {
// 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 = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('Error connecting to MySQL server.');

// Write the data to the database
$query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score', '$screenshot')";
mysqli_query($dbc, $query);

// 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><br />';
echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
echo '<p><a href="index.php">&lt;&lt; Back to high scores</a></p>';

// Clear the score data to clear the form
$name = "";
$score = "";
$screenshot = "";

mysqli_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="32768" />
<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="score"> Screenshot:</label>
<input type="file" id="screenshot" name="screenshot" />
<hr />
<input type="submit" value="Add" name="submit" />
</form>

</body>
</html>








Can someone tell me what I need to do to fix this?

This post has been edited by Big Lew: 28 May 2014 - 06:49 PM

0

#2 User is offline   lemuel 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 576
  • Joined: 13-April 09

Posted 29 May 2014 - 08:18 AM

Add lines as shown, the output should lead you to the problem
$screenshot_type = $_FILES['screenshot']['type'];
$screenshot_size = $_FILES['screenshot']['size'];
echo "The uploaded file is named $screenshot";
echo "The file type is $screenshot_type";
echo "The file size is $screenshot_size";

0

#3 User is offline   Big Lew 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 03-January 14

Posted 29 May 2014 - 04:45 PM

I tried that and the output displays that the file is a giff and well below the 32K limit. I don't know what the problem is.

This post has been edited by Big Lew: 29 May 2014 - 05:13 PM

0

#4 User is offline   lemuel 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 576
  • Joined: 13-April 09

Posted 29 May 2014 - 08:05 PM

Paste the exact output here.
0

#5 User is offline   Big Lew 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 03-January 14

Posted 30 May 2014 - 05:24 AM

View Postlemuel, on 29 May 2014 - 08:05 PM, said:

Paste the exact output here.



The uploaded file is named phizsscore.gif. The file type is image/gif. The file size is 12244.

The screen shot must be a GIF, JPEG, or PNG image file no greater than 0 KB in size.
0

#6 User is offline   lemuel 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 576
  • Joined: 13-April 09

Posted 30 May 2014 - 08:08 AM

Do you see anything unusual here?

The screen shot must be a GIF, JPEG, or PNG image file no greater than 0 KB in size.
0

#7 User is offline   Big Lew 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 03-January 14

Posted 30 May 2014 - 04:33 PM

View Postlemuel, on 30 May 2014 - 08:08 AM, said:

Do you see anything unusual here?

The screen shot must be a GIF, JPEG, or PNG image file no greater than 0 KB in size.



Thank you for your help. I don't know how I missed that. After you pointed that out, I realized that I forgot to define the value for GW_MAXFILESIZE in the appvars.php file. You have saved me a lot of aggravation.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users