Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 22nd, 2009, 11:05 PM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default File upload problems

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">&lt;&lt; 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.
 
Old May 7th, 2009, 02:56 PM
Authorized User
 
Join Date: Nov 2008
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Cool i think i figured it out.

create a directory called "userimages" in the directory which contains your php script. That's it.
 
Old May 8th, 2009, 09:41 PM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply. I did have a file called 'userimages'. But as it turns out, on my live server the permissions were not set for public write access. Once that was fixed I was able to upload with no problems.
 
Old May 10th, 2009, 06:37 AM
Authorized User
 
Join Date: Nov 2008
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Cool ok.

Thanks for your information.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems regarding upload functionality jacob Javascript 1 January 2nd, 2007 06:38 AM
asp file upload script having problems with MYSQL paulmcn Classic ASP Databases 0 September 16th, 2005 01:26 PM
Whole Folder upload(Multi file Upload) ramasamy_rams XML 1 September 9th, 2005 12:43 PM
Chapter 7 file upload problems GranDad BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 August 27th, 2005 02:29 PM
File upload problems in PHP 4.3.2 clem_c_rock Pro PHP 2 November 1st, 2004 10:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.