O'Reilly Forums: Uploading Files Other Than Images. - O'Reilly Forums

Jump to content

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

Uploading Files Other Than Images. Allowing users to add .pdf,.xls, .doc(x), to the server.

#1 User is offline   Samseen 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 17-September 12

Posted 28 November 2012 - 03:47 AM

Hi fellas, I'm writing an application that will allow a user to upload a .pdf, .doc or .xls file. After a lot of efforts, I had to edit the guitar wars application. Now that i've edited the scripts, i still get an error whenever I upload that: The screen shot must be a GIF, JPEG, or PNG image file no greater than 3200000 KB in size.

The addscore script that should allow .pdf and co:
<!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
  require_once('appvars.php');
  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_type == 'image/pdf') || ($screenshot_type == 'image/doc')) &&
      ($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);

          // 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 . '<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="screenshot">Screen shot:</label>
    <input type="file" id="screenshot" name="screenshot" />
    
    <hr /> 
    <input type="submit" value="Add" name="submit" />

  </form>

</body>

</html>



The appvars.php code:
<?php
// Define application constants
define('GW_UPLOADPATH', 'images/');
define('GW_MAXFILESIZE', '3276800000');  //32KB
?>


Thank you in anticipation!
0

#2 User is offline   drewdin 

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

Posted 29 November 2012 - 06:44 AM

how big is the file you are uploading? you have a 3mb limit set, what if you just remove the filters to see if it takes the file that way too.
0

#3 User is offline   Samseen 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 17-September 12

Posted 30 November 2012 - 12:07 AM

View Postdrewdin, on 29 November 2012 - 06:44 AM, said:

how big is the file you are uploading? you have a 3mb limit set, what if you just remove the filters to see if it takes the file that way too.


It's not that big. A jpeg file of 1,12 MB (1.176.711 bytes). I've also tried removing the filters as you suggested. I'm still on it fellas. I just won't believe it won't upload. Thanks for your consideration.
0

#4 User is offline   drewdin 

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

Posted 30 November 2012 - 10:37 AM

you should check you MIME types:

($screenshot_type == 'image/pdf') || ($screenshot_type == 'image/doc')


do a print_r on your script and see what your getting when you try to upload it

This post has been edited by drewdin: 30 November 2012 - 10:37 AM

0

#5 User is offline   Samseen 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 17-September 12

Posted 06 December 2012 - 12:41 AM

View Postdrewdin, on 30 November 2012 - 10:37 AM, said:

you should check you MIME types:

($screenshot_type == 'image/pdf') || ($screenshot_type == 'image/doc')


do a print_r on your script and see what your getting when you try to upload it


Thank you drewdin i got it fixed. Now I think the book does not provide a topic on how to download a file and that is what i'm up to now. Can you kindly suggest a way to learn that?
0

#6 User is offline   drewdin 

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

Posted 06 December 2012 - 11:55 AM

I know this is going to be vague but there are a few ways of doing it, some very easy and some very hard. It depends on how your storing the files and how you want people to get to them. I would google or check stackoverflow for some answers.

I did a quick search on stack and got like 50 results. Take a look
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