O'Reilly Forums: (Another) Chapter 5 Problem - O'Reilly Forums

Jump to content

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

(Another) Chapter 5 Problem Problem moving images

#1 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 31
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

  Posted 05 October 2012 - 04:27 AM

Hello let me start by saying that I am really enjoying HFPHP! It's the first time I believe that I've begun to grasp this topic after a long time of wanting to.

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:

Attached Image: hfphp.png

This is what index.php looks like when it loads:

Attached Image: HFPHP2.png

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

0

#2 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 31
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

  Posted 05 October 2012 - 04:37 AM

I greatly apologize. I tried to edit the last post i made on this topic (which was thrown together hurriedly on a train commute) and when I got to work I tried to finish up the post with a little more info to help diagnose the problem. Somehow I ended up posting the same problem twice. Is there any way I could ask a mod to delete the older post from myself (bluethundr)at today, 07:01 AM? Or could someone let me know how to do it myself? Again apologies. And by the way I love the head first way of learning.

This post has been edited by bluethundr: 05 October 2012 - 05:15 AM

0

#3 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 608
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 05 October 2012 - 06:07 AM

look at this
File does not exist: /var/www/html/examples/ch05/initial/guitarwars-mod/unverified.gif


and them this line
define('GW_UPLOADPATH', 'images/');


maybe...
guitarwars-mod/images/


would produce your images, im not sure without seeing your folder structure
0

#4 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 31
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

Posted 05 October 2012 - 07:22 AM

View Postdrewdin, on 05 October 2012 - 06:07 AM, said:

look at this
File does not exist: /var/www/html/examples/ch05/initial/guitarwars-mod/unverified.gif


and them this line
define('GW_UPLOADPATH', 'images/');


maybe...
guitarwars-mod/images/


would produce your images, im not sure without seeing your folder structure



Hello and thanks for your reply. Perhaps there is some confusion in my post. I apologize for that and I will edit it to try to eliminate the confusion. I was trying to indicate that I had attempted using a couple of different settings for the GW_UPLOADPATH constant.

This is the current setting I am using for GW_UPLOADPATH and works to move the files to the right place but not to display them:

<?php
  // Define appplication constants
  define('GW_UPLOADPATH', '/var/www/html/examples/ch05/initial/guitarwars-mod/images/');
?>


Where I give the full path to the images folder.


And I give my folder structure here:

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



But the problem is not that move_uploaded_files() is not moving the files to the right location. They are thanks to me setting the full path to the images folder in my GW_UPLOADPATH constant.

And as you can see from my post the images are making it to the right location:

[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



The problem is rather that when the addscore.php script goes to display the images it thinks to look in the
 /var/www/html/var 
directory which is absolutely baffling to me.

And index.php is looking for unregistered.gif in
/var/www/html/examples/ch05/initial/guitarwars-mod/unverified.gif
according to the logs. Even tho the index.pp script uses the same GW_UPLOADPATH constant to display the image with this line:

 echo '<td><img src="' . GW_UPLOADPATH . $row['screenshot'] . '" alt="Score image" /></td></tr>';


That these two lines use to correctly move the uploaded files to the right location.

$target = GW_UPLOADPATH . $screenshot;
      if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target))


:blink:



So to clarify the problem is not that the files are not being moved to the right location, because they are! But that the scripts are looking in the wrong place to display them. And that I do not understand and am hoping somebody can help me out on. :)
0

#5 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 31
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

Posted 05 October 2012 - 10:19 AM

View Postlemuel, on 05 October 2012 - 09:00 AM, said:

This line:
echo '<td><img src="unverified.gif" alt="Unverified score" /></td></tr><br /><br />';

tells index.php to look for "unverified.gif" in the same directory where index.php resides. I think you want "images/unverified.gif".


Lemuel,

Thank you so much! That worked like a charm! But only for addscore.php. and adding the 'unverified' image to index.php Which is cool and I'm excited about that.

What I did tho was alter your advice only somewhat. I preferred to keep my values in static CONSTANTS so that's what I did.

I changed appvars.php to this:

  
<?php
  // Define appplication constants
  define('GW_UPLOADPATH', 'images/');
?>


And kept the line in addscore.php like this:
echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';


And this was the result:

Attached Image: guitarshot.png

A happy thing!

However my remaining problem is with index.php. Since the change I made to appvars.php all the 'unverified' images show up on index.php thanks to this line:

echo '<td><img src="' . GW_UPLOADPATH . 'unverified.gif' . '" alt="Score image" /></td></tr><br /><br />';

But the puzzling this is that even tho I can successfully post images on adddscore.php, ALL of the images show the 'unverified' image on index.php.

Attached Image: robertpalmer.png

This is the line from index.php that as far as I think should display the success image:

if (is_file($row['screenshot']) && filesize($row['screenshot']) > 0){
	echo '<td><img src="' . GW_UPLOADPATH . $row['screenshot'] . '" alt="Score image" /></td></tr>';



Here's the whole php script for index.php just in case a logic error is contributing to this problem.

<?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="' . GW_UPLOADPATH . 'unverified.gif' . '" alt="Score image" /></td></tr><br /><br />';
	}
  echo '</table>';
  }
  mysqli_close($dbc);
?>


It's a short amount of code so I hope you don't mind me posting it again. I'd love to get to the bottom of this mystery and I'd be very thankful for your help!.

This post has been edited by bluethundr: 05 October 2012 - 11:17 AM

0

#6 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 31
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

Posted 05 October 2012 - 11:51 AM

View Postlemuel, on 05 October 2012 - 11:31 AM, said:

Think about this line from index.php:
if (is_file($row['screenshot']) && filesize($row['screenshot']) > 0){

In what directory is php looking for the file "$row['screenshot']"?


Bingo! It's looking in the web app directory not the images directory. Sorry I didn't see that and think it through. Hopefully seeing the outcome of statements like that will come with time as I develop my PHP skills!

What was needed was this:

if (is_file( GW_UPLOADPATH . $row['screenshot']) && filesize( GW_UPLOADPATH . $row['screenshot']) > 0){


That fixed it. The entire Guitar Wars app works fine for me now. ONWARD HO with HFPHP! Great book! Learning tons!

Best!
Tim

This post has been edited by bluethundr: 05 October 2012 - 11:52 AM

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