File upload

Reply

Join Date: Dec 2007
Posts: 10
Reputation: tie372 is an unknown quantity at this point 
Solved Threads: 0
tie372 tie372 is offline Offline
Newbie Poster

File upload

 
0
  #1
Mar 23rd, 2008
I have a form and upload script so people can upload games to my website. The form code:

  1. echo "<form action=newgame.php enctype=multipart/form-data><table border=0 method=post bgcolor=1a1a1a><tr><td width=70%>
  2. game title</td>
  3. <td><input type=text name=title maxlength=48></input></td>
  4. </tr>
  5. <tr><td>summary</td><td><textarea name=summary rows=5 cols=30></textarea></tr>
  6. <tr><td>genre</td><td>
  7. <select name=genre>
  8. <option value=action>action</option>
  9. <option value=adventure>adventure</option>
  10. <option value=arcade>arcade</option>
  11. <option value=other>other</option>
  12. <option value=puzzle>puzzle</option>
  13. <option value=RPG>role-playing</option>
  14. <option value=sports>sports</option>
  15. <option value=strategy>strategy</option>
  16. <option value=widget>widget</option></td></tr>
  17. <tr><td>price (coming soon)</td><td>
  18. </td></tr>
  19. <tr><td>screenshot</td><td>
  20. <input type=file name=screen></input></td></tr>
  21. <tr><td>file</td><td>
  22. <input type=file name=game></input></td></tr>
  23. <tr><td></td><td><input type=submit name=submit value=add></input>";
  24. ?>

And here's the upload part...

  1. <?php
  2. session_start();
  3. include "header.html";
  4. include "db.php";
  5.  
  6. if(!isset($_SESSION[user])){include "loginform.html"; echo "you must be logged in to view this page<br><br>if you have an account, log in above<br><br>if not, <a href=accountcreate.php>create an account</a>"; exit(); }
  7. else
  8. if(isset($_SESSION[user])){
  9. echo "<sup><div align=right>you're logged in as $_SESSION[user] | <a href=logout.php>log out</a></sup></div>";
  10. }
  11. $sql=mysql_query("SELECT * FROM games ORDER BY id DESC");
  12. $id=mysql_num_rows($sql);
  13. $id=$id+1;
  14.  
  15. $name=$_POST[title];
  16. $summary=$_POST[summary];
  17. $genre=$_POST[genre];
  18.  
  19. //screenshot
  20. $screenname=$_FILES['screen']['name'];
  21. $screensize=$_FILES['screen']['size'];
  22. $screentype=$_FILES['screen']['type'];
  23.  
  24. //gamefile
  25. $gamename=$_FILES['game']['name'];
  26. $gamesize=$_FILES['game']['size'];
  27. $gametype=$_FILES['game']['type'];
  28.  
  29. print_r($_FILES); //for debugging
  30. //screenshot
  31. //basic filter
  32. if(empty($screenname)){echo "no file name was indicated. hit back and try again"; exit();}
  33. if($screensize>3000000){echo "your file is too big. hit back and try again"; exit();}
  34.  
  35. if($screentype == "image/gif" || $screentype =="image/jpg" || $screentype =="image/png" || $screentype =="image/jpeg" || $screentype =="image/bmp"){
  36.  
  37. $location="screens/" . $id;
  38.  
  39. move_uploaded_file($_FILES['screen']['tmp_name'], $location);
  40.  
  41. }
  42.  
  43. //screenshot
  44. //game
  45.  
  46.  
  47.  
  48. if(empty($gamename)){echo "no file name was indicated. hit back and try again"; exit();}
  49. if($gamesize>26214400){echo "your file is too big. hit back and try again"; exit();}
  50.  
  51.  
  52.  
  53. $location="games/" . $id;
  54. echo $location;
  55.  
  56. move_uploaded_file($_FILES['game']['tmp_name'], $location);
  57.  
  58.  
  59. //game
  60.  
  61. $sql=mysql_query("INSERT INTO games(name, summary, price, genre, user, rating) VALUES ('$name','$summary','0','$genre','$_SESSION[user]','0')");
  62. echo "your game has been created.<br><img src=screens/". $id ."><br><br>";
  63. ?>

The screenshot uploading works fine, as expected, but the game file doesn't work. Only the name is recorded, but the filetype and size are left blank.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

Re: File upload

 
0
  #2
Apr 2nd, 2008
Hi there, I don't see that you'd be using $gametype or $gamesize. Could you please add some code that proves that they are empty indeed?

Also did you try displaying $_FILES['game']? Add var_dump($_FILES['game']); to see what's in there.

Your HTML code doesn't end the table and the form. Also you should use attribute="value" notation and not attribute=value. Lazy programming is the best way to introduce bugs that are very difficult to trace.

Because your HTML doesn't contain any variables, you don't need that echo "" wrapper around it. Just quit PHP source code with ?> have the HTML code and then enter PHP source code again with <?php

You shouldn't use $_POST[title] - $_POST["title"] is better (title would first be tried as a constant).
If you place
  1. ini_set("display_errors", true);
  2. error_reporting(E_ALL);
you'll see what other mistakes you've made.
Petr 'PePa' Pavel

The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC