joshgarrod Posted January 29, 2009 Share Posted January 29, 2009 Hi all, I have a form which is supposed to submit information to the database about an item and upload an image with it, it submits the data fine but the image doesn't upload, it gets as far as renaming the file and moving it to a temp file but no further, where am I going wrong? PHP: <?php if (isset ($_FILES['fupload'])){ $filename = $_FILES['fupload']['name']; $randomdigit = rand(0000,9999);//create random digit $newfilename = $randomdigit.$filename;//make new file name with random digit //printing file information print "<table>"; print "<tr><td>Original Name:</td><td> ". $_FILES['fupload']['name']."</td></tr>"; print "<tr><td>New Name:</td><td> ".$newfilename."</td></tr>"; print "<tr><td>Size: </td><td>". $_FILES['fupload']['size']."</td></tr>"; print "<tr><td>Temp Name: </td><td>". $_FILES['fupload']['tmp_name']."</td></tr>"; print "<tr><td>Type: </td><td>". $_FILES['fupload']['type']. "</td></tr>"; print "<tr><td>Error: </td><td>". $_FILES['fupload']['error']. "</td></tr>"; print "</table>"; //checking the type of file, if it is image it will display it if ($_FILES['fupload']['type'] == "image/jpeg"){ $source = $_FILES['fupload']['tmp_name']; $target = "uploads/".$newfilename; move_uploaded_file($source, $target); // or die ("Couldnt copy"); //displaying the image $imagesize = getImageSize($target); $imgstr = "<p><img width=\"$size[0]\" height=\"$size[1]\" "; $imgstr .= "src=\"$target\" alt=\"uploaded image\" ></p>"; $yoursite = "http://www.mysite.co.uk/"; $imagepath = $yoursite.$target; print $imgstr; print "The link to your image is: ".$yoursite.$target;//link to the image } } error_reporting (E_ALL ^ E_NOTICE); $usr = "ust"; $pwd = "pass"; $db = "db"; $host = "host"; # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } if ($_POST['submit']) { $title = mysql_real_escape_string($_POST['title']); $descr = mysql_real_escape_string($_POST['descr']); $partno = mysql_real_escape_string($_POST['partno']); $price = mysql_real_escape_string($_POST['price']); $avail = mysql_real_escape_string($_POST['avail']); $image = mysql_real_escape_string($_POST['image']); $SQL = "INSERT INTO spares"; $SQL .= " (title, descr, partno, price, avail, image) VALUES "; $SQL .= " ('$title','$descr','$partno','$price', '$avail', '$image') "; $result = mysql_db_query($db,$SQL,$cid); if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("<P><B>Congratulations... you have successfully added a new part to the stock list</B></P>\n"); } ?> HTML form: <form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF'] ?> " method ="post"> <div class="input_row"> <div class="input_titles">Title:</div> <div class="input_box"><INPUT NAME="title" TYPE="text" id="title" VALUE="" SIZE=50> /div> </div> <div class="input_row"><div class="input_titles">Description:<br /><br />(Use this space to add further descriptive text about this spare part e.g. dimensions etc.)</div><div class="input_box"><textarea name="descr" cols="50" rows="10" id="descr" type="text" value="" /></textarea></div></div> <div class="input_row"><div class="input_titles">Part number:</div><div class="input_box"><input name="partno" type="text" id="partno" value="" size="10" /></div></div> <div class="input_row"><div class="input_titles">Price (£) :</div><div class="input_box"><input name="price" type="text" id="price" value="" size="15" />(number only, no commas or pound signs)</div></div> <div class="input_row"><div class="input_titles">In stock:</div><div class="input_box"><input name="avail" type="text" id="avail" value="" size="4" />(number only)</div></div> <input name="image" type="hidden" value="<?php $imagepath ?>" /> <input type = "hidden" name="MAX_FILE_SIZE" value = "102400"> <div class="input_row"><div class="input_titles">Select image:</div><div class="input_box"><input type = "file" name = "fupload"></div></div> <div class="input_row"><div class="input_titles"><input name="submit" type="submit" value="Add part" /></div></div> </FORM> Link to comment https://forums.phpfreaks.com/topic/142942-image-upload-and-data-submission-in-one-form/ Share on other sites More sharing options...
AdRock Posted January 29, 2009 Share Posted January 29, 2009 try this...it is old code i used ages but it works //variables for setting up paths to image and size $idir = "../images/gallery/full/"; // Path To Images Directory $tdir = "../images/gallery/thumbs/"; // Path To Thumbnails Directory $twidth = "100"; // Maximum Width For Thumbnail Images $theight = "75"; // Maximum Height For Thumbnail Images if (isset ($_FILES['fupload'])){ //upload the image to tmp directory $url = $_FILES['fupload']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload']['tmp_name'], "$idir" . $_FILES['fupload']['name']); // Move Image From Temporary Location To Permanent Location if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image //create the new thumbnail $simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From $currwidth = imagesx($simg); // Current Image Width $currheight = imagesy($simg); // Current Image Height if ($currheight > $currwidth) { // If Height Is Greater Than Width $zoom = $twidth / $currheight; // Length Ratio For Width $newheight = $theight; // Height Is Equal To Max Height $newwidth = $currwidth * $zoom; // Creates The New Width } else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) $zoom = $twidth / $currwidth; // Length Ratio For Height $newwidth = $twidth; // Width Is Equal To Max Width $newheight = $currheight * $zoom; // Creates The New Height } $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail imagetruecolortopalette($simg, false, 256); // Create New Color Pallete $palsize = ImageColorsTotal($simg); for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use } imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "$tdir" . $url); // Saving The Image imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image print 'Image thumbnail created successfully.'; // Resize successful } else { print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed } } Link to comment https://forums.phpfreaks.com/topic/142942-image-upload-and-data-submission-in-one-form/#findComment-749502 Share on other sites More sharing options...
joshgarrod Posted January 29, 2009 Author Share Posted January 29, 2009 unfortunately that is a little over comprehensive for what I need but thank you for the reply. Link to comment https://forums.phpfreaks.com/topic/142942-image-upload-and-data-submission-in-one-form/#findComment-749519 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.