saireuh Posted November 2, 2009 Share Posted November 2, 2009 Hey everyone! I've been trying to teach myself php & mysql stuff and to do so I've done some online tutorials and bought a couple books to help me along. I'm so lost with these 2 php files... it has to do with uploading high scores and images to a database (and then moving those images from tmp file to a permanent location). I was thinking that maybe this was a permissions problem or maybe just a tiny error in the code that I'm not catching... When I go to the addscore.php to upload it will give me an error message and then on index.php NONE of the images are displayed. (there are a couple other .phps associated with these two but they are the main ones that I'm concerned about.) Would any of you guys have an idea? Please help! addscore.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 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_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! It will be reviewed and added to the high score list as soon as possible.</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 = ""; $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="<?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">Screen shot:</label> <input type="file" id="screenshot" name="screenshot" /> <hr /> <input type="submit" value="Add" name="submit" /> </form> </body> </html> and 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 require_once('appvars.php'); require_once('connectvars.php'); // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // Retrieve the score data from MySQL $query = "SELECT * FROM guitarwars ORDER BY score DESC, date ASC"; $data = mysqli_query($dbc, $query); // Loop through the array of score data, formatting it as HTML echo '<table>'; $i = 0; while ($row = mysqli_fetch_array($data)) { // Display the score data if ($i == 0) { echo '<tr><td colspan="2" class="topscoreheader">Top Score: ' . $row['score'] . '</td></tr>'; } 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>'; if (is_file(GW_UPLOADPATH . $row['screenshot']) && filesize(GW_UPLOADPATH . $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="Unverified score" /></td></tr>'; } $i++; } echo '</table>'; mysqli_close($dbc); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/ Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 The error messages normally contain some insightful wisdom, would you mind sharing them ? Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949051 Share on other sites More sharing options...
PFMaBiSmAd Posted November 2, 2009 Share Posted November 2, 2009 Ditto... it will give me an error message Ummm. Unless you share what that error message was, no one can help you with the specific problem because the message would pin down which of the dozen possible problems you are actually having. Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949054 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 Okay within the past few minutes I was able to stop the error messages when I attempt to submit a high score (looking at the provided 'final' version of the code, which it itself has errors)... BUT... On addscore.php when I load it for the first time, I get the error message "Please enter all of the information to add your high score." as if it already checked for info to be in the fields? Would this have to do with a problem with the 'if (isset($_POST['submit'])' part of the code? And then the error that occurs on index.php... when the file is uploaded .. I cannot find it's 'tmp' folder and index.php is not displaying any image at all. So my attempt to move the img from tmp folder to the image folder is apparently failing... Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949056 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 It's this line if (!empty($name) && !empty($score) && !empty($screenshot)) { I would guess its the !empty($screenshot) part, that deals with the file being sent, add var_dump($_FILES); to get some more info eg $screenshot_type = $_FILES['screenshot']['type']; $screenshot_size = $_FILES['screenshot']['size']; //HERE var_dump($_FILES); EDIT: look at $_FILES['screenshot']['error'] if its NOT 0 (zero) then the file failed to upload of course post that info Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949059 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 Hmm okay I got this after attempting to submit a file with the var_dump($_FILES): array(1) { ["screenshot"]=> array(5) { ["name"]=> string(15) "nevilsscore.gif" ["type"]=> string(9) "image/gif" ["tmp_name"]=> string(14) "/tmp/phpWV5BW4" ["error"]=> int(0) ["size"]=> int(12214) } } I'm unsure why the !empty($screenshot) would be the problem part... I thought that this is saying basically if there is a screenshot that is uploaded, move it to the images folder? Among other things? Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949064 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 Humm.. okay.. i guessed wrong.. can you also do a var_dump($_POST); Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949066 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 (By the way, thanks so much for helping ) After adding that I got: array(1) { ["screenshot"]=> array(5) { ["name"]=> string(15) "kennysscore.gif" ["type"]=> string(9) "image/gif" ["tmp_name"]=> string(14) "/tmp/phpQaj7a8" ["error"]=> int(0) ["size"]=> int(12434) } } array(4) { ["MAX_FILE_SIZE"]=> string(5) "32768" ["name"]=> string(11) "Scotty Ryan" ["score"]=> string( "99999999" ["submit"]=> string(3) "Add" } Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949068 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 Okay.. I don't see how your getting the error message "Please enter all of the information to add your high score." Can you confirm this please also just as a double check, try adding some checkpoints ie (note the pass #) if (!empty($name) && !empty($score) && !empty($screenshot)) { echo "pass1<br>\n"; if ((($screenshot_type == 'image/gif') || ($screenshot_type == 'image/jpeg') || ($screenshot_type == 'image/pjpeg') || ($screenshot_type == 'image/png')) && ($screenshot_size > 0) && ($screenshot_size <= GW_MAXFILESIZE)) { echo "pass2<br>\n"; if ($_FILES['screenshot']['error'] == 0) { echo "pass3<br>\n"; // Move the file to the target upload folder $target = GW_UPLOADPATH . $screenshot; if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) { echo "pass4<br>\n"; EDIT: Oh and your Welcome Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949071 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 The error I was receiving, as if I already submitted the script, was fixed. So that's no longer displayed when I first load addscore.php... After adding the checkpts I got this: array(1) { ["screenshot"]=> array(5) { ["name"]=> string(15) "nevilsscore.gif" ["type"]=> string(9) "image/gif" ["tmp_name"]=> string(14) "/tmp/phpWKXcWx" ["error"]=> int(0) ["size"]=> int(12214) } } array(4) { ["MAX_FILE_SIZE"]=> string(5) "32768" ["name"]=> string(5) "Saire" ["score"]=> string(7) "9394999" ["submit"]=> string(3) "Add" } pass1 pass2 pass3 pass4 So I'm assuming everything is acting as it should? But I can't find the tmp folder, the img files... I set all of the file permissions to 777 and I'm realizing that the images ARE being moved to the image/ folder.. but index. php cannot display it!!! www.gamerecoil.com/Sara/PHP6/addscore.php www.gamerecoil.com/Sara/PHP6/index.php Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949077 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 I ran a test on your site and didn't get Pass 4! array(1) { ["screenshot"]=> array(5) { ["name"]=> string(19) "Windows Vista 9.JPG" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phpSbE7Zu" ["error"]=> int(0) ["size"]=> int(6463) } } array(4) { ["MAX_FILE_SIZE"]=> string(5) "32768" ["name"]=> string(9) "MadTechie" ["score"]=> string(10) "1234567890" ["submit"]=> string(3) "Add" } pass1 pass2 pass3 Warning: move_uploaded_file(images/Windows Vista 9.JPG) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/recoil/public_html/Sara/PHP6/addscore.php on line 38 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpSbE7Zu' to 'images/Windows Vista 9.JPG' in /home/recoil/public_html/Sara/PHP6/addscore.php on line 38 Sorry, there was a problem uploading your screen shot image. Also it seams like a permissions problem, as the images are being listed but are not viewable.. set the permission to 755, (as some hosts providers lock out 777 access) EDIT: are you using any mod-rewrite's ? Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949087 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 Okay I changed the permissions 755. To be honest the main site, gamerecoil, is using mod-rewrites but I'm not sure if that could affect my folder? I guess its possible. Is there anything I could do? If not I could try to host my folder somewhere else. Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949095 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 Well its seams to be Permission problems Permission denied also if you try something unPHP related you still get problems ie, try to open a image form here http://www.gamerecoil.com/Sara/PHP6/images/ EDIT: oh boy.. its 3:15am i to be up in 4 hours!.. I'm going to get some sleep.. try another host.. also look at wampserver, xxamp as you can run them from windows client, (nice for dev testing) Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949100 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 Grr well... no dice if it's set at 755 OR 777.... The mod re-write wouldn't affect permissions? I'm not quite sure if there is any way to find the right permissions setting... Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949102 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 No a mod re-write wouldn't effect permission but it could cause strange behaviour, for example you could have a mod that points images/(.*)\.gif to images.php?name=\1 while that could be intended for only one folder it would effect all! including http://www.gamerecoil.com/Sara/PHP6/images/ I don't think that is the case but i wouldn't rule it out. But testing on another server or locally would help rule out most coding errors. Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949108 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 Okay... I added the site to a different server with no php previously on it... no mod re-writes or anything. www.allhinges.com/sara/PHP6/index.php Now the images are displayed there BUT when I attempt to upload an image I get this error: array(1) { ["screenshot"]=> array(5) { ["name"]=> string(15) "nevilsscore.gif" ["type"]=> string(9) "image/gif" ["tmp_name"]=> string(14) "/tmp/phpsArkp1" ["error"]=> int(0) ["size"]=> int(12214) } } array(4) { ["MAX_FILE_SIZE"]=> string(5) "32768" ["name"]=> string(6) "Sara H" ["score"]=> string( "99999999" ["submit"]=> string(3) "Add" } pass1 pass2 pass3 Warning: move_uploaded_file(images/nevilsscore.gif) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/hinges/public_html/sara/PHP6/addscore.php on line 38 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpsArkp1' to 'images/nevilsscore.gif' in /home/hinges/public_html/sara/PHP6/addscore.php on line 38 Sorry, there was a problem uploading your screen shot image. Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949132 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 Oh my goodness! FINALLY! It's working! Thank you SO SO much for suggesting the mod rewrite.. that was creating the problem. After I changed the file permissions on the image folder at the new place I'm hosting it at, it worked! You have no idea how much aggravation this caused me. It was in a tutorial book for Pete's sake, it should have been easier than this. Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949141 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 Cool, The script looked fine, however it maybe worth getting a local copy.. (it easy to install /start /stop) if you use WAMPSERVER2. Can you click topic solved (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949441 Share on other sites More sharing options...
acialk Posted November 2, 2009 Share Posted November 2, 2009 Oh my goodness! FINALLY! It's working! Thank you SO SO much for suggesting the mod rewrite.. that was creating the problem. After I changed the file permissions on the image folder at the new place I'm hosting it at, it worked! You have no idea how much aggravation this caused me. It was in a tutorial book for Pete's sake, it should have been easier than this. I've had similar problems myself following tutorials from books. It's quite common. Which O'Reilly book are you learning from out of interest? Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949731 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 O'Reilly's Head First PHP & MySQL. It's a uniquely written book and it's a very easy read... but I've come across a few places where the errors (or unmentioned steps/problems in a tutorial) really hold me up. Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949752 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 I have a PDF of that book, a friend also learning PHP asked me for help him, the problem is if the books to complex then its overwhelming, if its too basic then your unprepared for problems. if that said on first review the book seams quite good. Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949758 Share on other sites More sharing options...
saireuh Posted November 2, 2009 Author Share Posted November 2, 2009 Yeah the book isn't too bad... not overly simple, it's just that when a problem occurs they tend to leave the bits out that help explain why. Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949781 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 But that becomes part of the fun okay not always, however debugging is a very important skill to learn.. so maybe they are forcing it on you of course any problems you can always post for help I'm happy to help those who wish to learn. Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-949787 Share on other sites More sharing options...
robertmulder Posted January 27, 2010 Share Posted January 27, 2010 I had the same problems with uploading images. I've struggled with permissions (XAMMP on Linux), with settings in php.ini, but nothing worked. I've included lots of echo statements to dump the values of the vars at runtime. I also included a infinite loop to check if the image file was actually uploaded to the temporary location, e.g. /tmp on Linux (if you do not change php.ini this is the default). But no images where stored there. Finally I changed GW_UPLOADPATH in appvars.php from a relative to a absolute path. This var nomally points to the relative location of my source files. I've hardcoded it to a fixed location on my Linux file structure with the proper permissions (r/w). Now the addscore.php script worked fine, but unforunately the index.php didn't. Images were still not displayed. The properties of the missing image files told me that the path was now the path to the location of my source file appended with the absolute path of GW_UPLOADPATH. And now I'm lost too. What else can I do to solve this problem with relative or absolute path settings? Quote Link to comment https://forums.phpfreaks.com/topic/179907-oreilly-tutorial-script-help-poor-girl-cant-figure-it-out/#findComment-1002319 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.