doddsey_65 Posted November 24, 2009 Share Posted November 24, 2009 I have an image upload script that works fine, but i decided to make another one for a dif section of the site. This time though i needed to include things like name and description aswell as the image to upload. So i copied the upload.php file and added a few more things in but when i click uppload it goes straight to a blank screen and does nothing. Is there something wrong wih my code? Form echo '<form enctype="multipart/form-data" action="uploader2.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="200000" /> <table border="0"> <tr><td>Username:</td><td> <input type="text" name="username" maxlength="60"><font color=#ff0000 size=1> * required</font> </td></tr> <tr><td>Render Name:</td><td> <input type="text" name="rendername" maxlength="50"><font color=#ff0000 size=1> * required</font> </td></tr> <tr><td>Description:</td><td> <input type="text" name="description" maxlength="50"><font color=#ff0000 size=1> * required</font> </td></tr> <br><br> <tr><td>Choose an Image:</td><td> <input name="userfile" type="file" id="userfile" /><br /> <input type="submit" value="Upload Render" name="upload2" id="upload2" /> </td></tr></table></form></p>'; echo '</div>'; upload2.php <?php ob_start(); include('header.php'); include('db.php'); $db=mysql_connect($db_host,$db_user,$db_pass) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db($db_name,$db); $uploadDir = 'uploads/'; if(isset($_POST['upload2'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $username = $_POST['username']; $render = $_POST['rendername']; $description = $_POST['description']; $filePath = $uploadDir . $fileName; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } if(!get_magic_quotes_gpc()) { $filePath = addslashes($filePath); } $sql = ("UPDATE publicgallery SET path='$filePath', username='$username', name='$rendername', description='$description', link='$filePath'") or die (mysql_error()); $result = mysql_query($sql, $db); echo '<br><br> <div id="page"> <div id="content"> <div class="post"> <p class="meta">Your image has been successfully uploaded <img src="images/img08.png" alt="bullet"></p> <div class="entry">'; echo 'Your new image will now be visible on the Public Gallery. Click <a href=public_gallery.php>here</a> to go back. </div></div></div>'; include('footer.php'); } ob_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/ Share on other sites More sharing options...
Deoctor Posted November 24, 2009 Share Posted November 24, 2009 i think at echo '<br><br> u have made a mistake.. close the echo and try it out.. Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-964562 Share on other sites More sharing options...
doddsey_65 Posted November 24, 2009 Author Share Posted November 24, 2009 the echo follows on to the lines below for the div and closes at the end of them Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-964563 Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2009 Share Posted November 24, 2009 Debug what you are getting by adding the following lines of code immediately after the first opening <?php tag in upload2.php - echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; print_r($_FILES); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-964567 Share on other sites More sharing options...
doddsey_65 Posted November 24, 2009 Author Share Posted November 24, 2009 cheers for the reply, heres what i came out with: POST:Array ( [MAX_FILE_SIZE] => 200000 [username] => doddsey_65 [rendername] => cell [upload] => Upload Render ) FILES:Array ( [userfile] => Array ( [name] => cell.jpg [type] => image/pjpeg [tmp_name] => /tmp/phpseGNuN [error] => 0 [size] => 4499 ) ) All looks ok to me here, can you see anything wrong? Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-964568 Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2009 Share Posted November 24, 2009 The form code you posted above is not the form you are actually using. The form code you posted is using upload2 as the name of your submit button and that is the name that your form processing code is testing for. The form you are actually using has a submit button named upload, so your form processing code is skipping over the if(isset($_POST['upload2'])){....} conditional code. Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-964570 Share on other sites More sharing options...
doddsey_65 Posted November 24, 2009 Author Share Posted November 24, 2009 okay im confused lol. The form i am using has a submit button like so: <input type="submit" value="Upload Render" name="upload2" id="upload2" /> and the code in upload2 for it is if(isset($_POST['upload2'])) which i thought was right Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-964572 Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2009 Share Posted November 24, 2009 This is from the printout you just posted - [upload] => Upload Render That clearly shows that the form is setting a submit button named upload Have you refreshed your form page in your browser after making any changes to it so that those changes would be reflected in the HTML? Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-964573 Share on other sites More sharing options...
doddsey_65 Posted November 24, 2009 Author Share Posted November 24, 2009 yeh i always refresh the page after editing files, but i understand what you mean now. is there anything else that could cause this? could it be due to the form type(multipart/form-data) or because there is another form(on a dif page) with the same names(eg userfile) Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-964577 Share on other sites More sharing options...
doddsey_65 Posted November 25, 2009 Author Share Posted November 25, 2009 anyone Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-965190 Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2009 Share Posted November 25, 2009 Browsers don't make up HTML code. The form you are browsing to that has an action attribute of action="uploader2.php" also has a submit button with a name attribute of name="upload" You would need resolve your different forms, actions, submit button names, and the names of the variables you use in the form processing code. Your description field in the form code you posted did not show up in the output from the print_r() instruction, so clearly (again) the form you are browsing to is not the form code you posted in this thread. The form code you did post in this thread produces the following output from the print_r() instructions (for all empty form fields) - POST:Array ( [MAX_FILE_SIZE] => 200000 [username] => [rendername] => [description] => [upload2] => Upload Render ) FILES:Array ( [userfile] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-965237 Share on other sites More sharing options...
doddsey_65 Posted November 25, 2009 Author Share Posted November 25, 2009 so basically change all of the variables in upload2.php so they are dif from the other upload form(upload.php) and the same in the forms. When i got rid of the upload.php and sed this one(upload2.php) it did work but i will try them both. Oh and when i say it did work it wasnt uploading or inserting the image details. I echoed the sql and it didnt display the filepath, so i echoed the $filepath and came up with nothing. Since the first upload script is gone i know this is nothing to do with it. When i set about doing this thing i thought it would be easy lol, just insert a few things in the db and recall them. How wrong was I. Anyway have ou any idea why $filepath is returning nothing? It worked fine in the first upload.php. <?php ob_start(); include('header.php'); include('db.php'); $db=mysql_connect($db_host,$db_user,$db_pass) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db($db_name,$db); $uploadDir = 'public/'; if(isset($_POST['upload2'])) { $fileName = $_FILES['userfile2']['name']; $tmpName = $_FILES['userfile2']['tmp_name']; $filePath = $uploadDir . $fileName; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } if(!get_magic_quotes_gpc()) { $filePath = addslashes($filePath); } $sql="INSERT INTO publicgallery (path, name, username, description) VALUES ('$filepath','$_POST[rendername]','$_POST[username]','$_POST[description]')"; $result = mysql_query($sql, $db); echo 'FILEPATH:(' .$filepath. ')'; echo $sql; echo '<br><br> <div id="page"> <div id="content"> <div class="post"> <p class="meta">Your image has been successfully uploaded <img src="images/img08.png" alt="bullet"></p> <div class="entry">'; echo 'Your new image will now be visible on the Public Gallery. Click <a href=public_gallery.php>here</a> to go back. </div></div></div>'; include('footer.php'); } ob_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182754-image-and-text-upload/#findComment-965239 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.