That said, I seem to be having a problem with a chapter that a few others here have had issues with which is chapter 5. I'm not honestly sure if the issue is with the code or with the way I have my server setup but I'm hoping that I can find the sage advice here to help me work through this problem.
I think in my case a good place to start would be to have a look at the apache error logs when I try to run the addscore.php or index.php scripts that I"ve taken from examples in the HFPHP boook.
[Fri Oct 05 07:43:34 2012] [error] [client xxx.xx.xx.x] File does not exist: /var/www/html/examples/ch05/initial/guitarwars-mod/unverified.gif, referer: http://centos/examples/ch05/initial/guitarwars-mod/ [Fri Oct 05 07:43:34 2012] [error] [client xxx.xx.xx.x] File does not exist: /var/www/html/favicon.ico [Fri Oct 05 07:43:36 2012] [error] [client xxx.xx.xx.x] File does not exist: /var/www/html/favicon.ico [Fri Oct 05 07:43:58 2012] [error] [client xxx.xx.xx.x] File does not exist: /var/www/html/var, referer: http://centos/examples/ch05/initial/guitarwars-mod/addscore.php [Fri Oct 05 07:43:58 2012] [error] [client xxx.xx.xx.x] File does not exist: /var/www/html/favicon.ico
The web server is a virtual machine running on my laptop (called centos) and this is the url I use to hit the site:
http://centos/examples/ch05/initial/guitarwars-mod
The directory:
/var/www/html/examples/ch05/initial/guitarwars-mod/
Is the correct one. I modified the initial guitarwars directory in the chapter 5 'initial' directory.
And images should be here:
/var/www/html/examples/ch05/initial/guitarwars-mod/images
Hopefully we'll see why this is happening when I post my code shortly. But what puzzles me the most is that apache gives me the following complaints in the error logs when I try to display an uploaded an image: with addscore.php or when index.php tries to reference an image I get the following messages:
[Fri Oct 05 06:10:56 2012] [error] [client xxx.xx.xx.x] File does not exist: /var/www/html/var, referer: http://centos/examples/ch05/initial/guitarwars-mod/addscore.php
With this log entry I have no idea how addscore.php indicates that the image file should be in
/var/www/html/var
[Fri Oct 05 07:51:55 2012] [error] [client xxx.xx.xx.x] File does not exist: /var/www/html/examples/ch05/initial/guitarwars-mod/unverified.gif, referer: http://centos/examples/ch05/initial/guitarwars-mod/index.php
And with this log entry I have no idea why index.php should be looking for
/var/www/html/examples/ch05/initial/guitarwars-mod/unverified.gif
When according to the way I have my constants setup it should be looking for unverified.gif here:
/var/www/html/examples/ch05/initial/guitarwars-mod/images/unverified.gif
Here is my code for addscrore.php:
<!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
error_reporting(E_ALL);
ini_set('display_errors', '1');
set_time_limit(2);
require_once('connectvars.php');
require_once('appvars.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'];
$screenshot_tmp = $_FILES['screenshot']['tmp_name'];
$screenshot_error = $_FILES['screenshot']['error'];
echo "screenshot name is:" . ' ' . $screenshot . '<br />';
echo "screenshot type is:" . ' ' . $screenshot_type . '<br />';
echo "screenshot size is:" . ' ' . $screenshot_size . '<br />';
echo "screenshot tmp is:" . ' ' . $screenshot_tmp . '<br />';
echo "screenshot error is:" . ' ' . $screenshot_error . '<br />';
if (!empty($name) && !empty($score) && !empty($screenshot)) {
// Move the file to the 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(DB_CONNECT_ERR . mysqli_connect_error() . DB_CONNECT_ERR_NO . mysqli_connect_errno());
// Write the data to the database
$query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score', '$screenshot')";
mysqli_query($dbc, $query)
or die( SQL_INSERT_ERR . mysqli_error($dbc) . SQL_ERR_NO . mysqli_errno($dbc) . QUERY_USED . $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 . '<br />';
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 = "";
mysqli_close($dbc);
}
}
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="screenshot">Screen Shot</label>
<input type="file" id="screenshot" name="screenshot" />
<hr />
<input type="submit" value="Add" name="submit" />
</form>
</body>
</html>
The very good news here is that it looks like move_uploaded_files moved the image files to the correct location. Here's an ls of my images file:
[root@centos:/var/www/html/examples/ch05/initial/guitarwars-mod] #ls -l images/ total 48 -rw-r--r--. 1 apache apache 25153 Oct 5 07:43 guitar-sign-rock-n-roll.jpeg -rw-r--r--. 1 apache apache 8881 Oct 5 07:18 r&r.jpg drwxrwxrwx. 2 apache ftp 4096 Oct 2 20:37 tmp -rwxrwxr-x. 1 apache ftp 2294 Oct 5 06:28 unverified.gif
This was the setting in my GW_UPLOADPATH constant that allowed this to happen:
<?php
// Define appplication constants
define('GW_UPLOADPATH', '/var/www/html/examples/ch05/initial/guitarwars-mod/images/');
?>Which makes me wonder even more why the script is looking in this weird location when it tries to display the images:
File does not exist: /var/www/html/var
And here is the code for index.php:
<!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 - High Scores</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Guitar Wars - High Scores</h2>
<p>Welcome, Guitar Warrior, do you have what it takes to crack the high score list? If so, just <a href="addscore.php">add your own score</a>.</p>
<hr />
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
set_time_limit(2);
require_once('connectvars.php');
require_once('appvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die(DB_CONNECT_ERR . mysqli_connect_error() . DB_CONNECT_ERR_NO . mysqli_connect_errno());
// Retrieve the score data from MySQL
$query = "SELECT * FROM guitarwars";
$data = mysqli_query($dbc, $query)
or die( SQL_SELECT_ERR . mysqli_error($dbc) . SQL_ERR_NO . mysqli_errno($dbc) . QUERY_USED . $query );
// Loop through the array of score data, formatting it as HTML
echo '<table>';
while ($row = mysqli_fetch_array($data)) {
// Display the score data
echo '<tr><td class="scoreinfo">';
echo '<span class="score">' . $row['score'] . '</span><br />';
echo '<strong>Name:</strong> ' . $row['name'] . '<br />';
echo '<strong>Date:</strong> ' . $row['date'] . '</td></tr><br />';
if (is_file($row['screenshot']) && filesize($row['screenshot']) > 0){
echo '<td><img src="' . GW_UPLOADPATH . $row['screenshot'] . '" alt="Score image" /></td></tr>';
} else {
echo '<td><img src="unverified.gif" alt="Unverified score" /></td></tr><br /><br />';
}
echo '</table>';
}
mysqli_close($dbc);
?>
</body>
</html>
As you can see above in addscore.php I am echoing all the values of the $_FILES array so that I can be sure that a file is being uploaded and processed. And I have my GW_UPLOADPATH set in both files and it's included in the appvars.php file with the statement
require_once('appvars.php'); in both. I also include connectvars.php in both scripts with the statement require_once('connectvars.php');Here is my appvars.php include
<?php
// Define appplication constants
define('GW_UPLOADPATH', '/var/www/html/examples/ch05/initial/guitarwars-mod/images/');
?>I have also tried the following values in 'GW_UPLOADPATH'
<?php
// Define appplication constants
define('GW_UPLOADPATH', 'images/');
?>And
<?php
// Define appplication constants
define('GW_UPLOADPATH', 'images');
?>The result in the logs is always the same. aaddscore.php always looks in the directory:
File does not exist: /var/www/html/var
And when I run index.php It always looks for the unverified.gif file in this location:
File does not exist: /var/www/html/examples/ch05/initial/guitarwars-mod/unverified.gif
Not that I think this plays into the problem too much, but here is my connectvars.php script:
<?php
//Define database connecton constants
define('DB_HOST', 'localhost');
define('DB_USER', 'xxxxxx');
define('DB_PASSWORD','xxxxxx');
define('DB_NAME', 'gwdb');
define('DB_CONNECT_ERR', "<center><strong/><font color='red' size='15'>Database connection failed.</font></center><br /><br />Error returned:</strong>". ' ');
define('DB_CONNECT_ERR_NO', '<br /><strong>Database connection error number:</strong>' . ' ');
define('SQL_INSERT_ERR', "<center><strong/><font color='red' size='15'>INSERT query failed to execute.</font></center><br /><br />Error returned:</strong>". ' ');
define('SQL_SELECT_ERR', "<center><strong/><font color='red' size='15'>SELECT query failed to execute.</font></center><br /><br />Error returned:</strong>". ' ');
define('QUERY_USED', "<br /><strong>Query used:</strong>" . ' ');
define('SQL_ERR_NO', "<br /><strong>Error no. returned:</strong>");
?>And this is what it looks like when I try to run the addscore.php script:
This is what index.php looks like when it loads:
Here are my php settings relating to image uploads on my web server. I'm hoping knowing this might help narrow down the issue:
file_uploads : file_uploads = On max_file_uploads = 20 upload_max_file_size : memory_limit : memory_limit = 128M max_execution_time : max_execution_time = 30 max_input_time : ; max_input_time max_input_time = 60 upload_tmp_dir : ;upload_tmp_dir = /tmp
Here are the how the permissions are set on my system /tmp directory. This is a VM on my laptop, security is no concern here so I'm leaving everything as open as possible. I would not do this with a real web server:
[root@centos:~] #ls -l / | grep tmp drwxrwxrwt. 5 apache ftp 12288 Oct 5 07:43 tmp
My document root for this app:
[root@centos:~] #ls -l /var/www/html/examples/ch05/initial/ | grep guitarwars-mod drwxrwxrwx. 3 apache ftp 4096 Oct 4 23:19 guitarwars-mod
My images folder:
[root@centos:~] #ls -l /var/www/html/examples/ch05/initial/guitarwars-mod | grep images drwxrwxrwx. 3 apache ftp 4096 Oct 5 07:43 images
Here is my document root for the Guitar Wars App:
[[root@centos:/var/www/html/examples/ch05/initial/guitarwars-mod] #ls -l total 24 -rwxrwxrwx. 1 apache ftp 3528 Oct 5 07:42 addscore.php -rwxrwxrwx. 1 apache ftp 132 Oct 5 06:30 appvars.php -rwxrwxrwx. 1 apache ftp 996 Oct 3 22:22 connectvars.php drwxrwxrwx. 3 apache ftp 4096 Oct 5 07:43 images -rwxrwxrwx. 1 apache ftp 1920 Oct 5 07:20 index.php -rwxrwxrwx. 1 apache ftp 159 Sep 30 23:14 style.css
And last but not least, here is my server environment:
[root@centos:~] #cat /etc/redhat-release CentOS release 6.3 (Final) [root@centos:~] #httpd -version Server version: Apache/2.2.15 (Unix) Server built: Feb 13 2012 22:31:42 [root@centos:~] #php -version PHP 5.3.14 (cli) (built: Jun 14 2012 21:47:38) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
Could it be the way that I have PHP configured? I'd appreciate any help you can give. Thank you!!
This post has been edited by bluethundr: 05 October 2012 - 05:48 AM













