+ Reply to Thread
Results 1 to 5 of 5

Thread: file upload trouble

  1. #1
    xadrieth is offline x10Hosting Member xadrieth is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    62

    file upload trouble

    I'm using "Headfirst PHP and MySQL" right now, and kin chapter 5, we need to create an script that allows us to upload a screenshot.

    But i keep on getting this error that all the info is no inputed when i enter score w/ screenshot.

    try it youself, http://twewy-fan.com/php/guitarwars/addscore.php

    Here is the code that is being used for it:
    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 the maximum file size contents
        define('GW_UPLOADPATH', 'images/');
    
      if (isset($_POST['submit'])) {
        // Grab the score data from the POST
        $name = $_POST['name'];
        $score = $_POST['score'];
        $screenshot = $_FILES['screenshot']['name'];
    
        if (!empty($name) && !empty($score) && !empty($screenshot)) {
          // Move the file to the target upload folder
          $target = GW_UPLOADPATH . $screenshot;
          // Connect to the database
          $dbc = mysqli_connect('localhost', '*******', '*******', '*******');
    
          // Write the data to the database
          $query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score', '$screenshot')";
          mysqli_query($dbc, $query);
          move_uploaded_file($_FILES['screenshot']['tmp_name'], $target);
    
          // 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">&lt;&lt; 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 enctpye="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>
    I already asked the Oreilly help forums, but i haven't gotten a good answer.

    Any help is appreciated.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: file upload trouble

    Typo: you set the enctpye, rather than enctype, attribute on the form.

    Also, sanitize your form input. As it stands, a malicious user could inject arbitrary SQL. The handling of file input should be safe as is.

  3. #3
    xadrieth is offline x10Hosting Member xadrieth is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    62

    Re: file upload trouble

    thank you, ill have to check it in the morning though (i have loads of typos).

    Also, ill be sure to sanitize it later, the book says it will cover i later.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: file upload trouble

    I was too terse in my first post. The typo is causing the problem, preventing the file from being uploaded. Correct it and the script will work.
    Last edited by misson; 05-19-2009 at 11:17 PM.

  5. #5
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: file upload trouble

    BTW, xadrieth,
    posting publicly those test scripts is not such a good idea. Very often, scripts that you are asked to write in beginner/intermediate tutorials and books are not prepared for the real world. There is very little security on those scripts. I would recommend that you setup a developing server on your computer to test and try scripts. I recommend XAMPP if you run on Windows, as it is a great development environment.

    Concerning security, always treat data as dangerous, until cleaned. So always sanitize your data. You might want to skip to the security part to read about that then come back where you were.
    Last edited by xav0989; 05-21-2009 at 05:34 PM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 09-20-2008, 02:12 AM
  2. i canot upload any file in my host
    By digital100 in forum Free Hosting
    Replies: 1
    Last Post: 07-15-2008, 09:24 PM
  3. Ftp File Upload (my Problem)
    By shaunNO2007 in forum Free Hosting
    Replies: 2
    Last Post: 07-13-2008, 08:13 AM
  4. help!!
    By retro-bliss in forum Free Hosting
    Replies: 25
    Last Post: 12-07-2007, 01:12 PM
  5. File Upload Function Problem
    By softz in forum Free Hosting
    Replies: 6
    Last Post: 11-26-2007, 06:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers