Jump to content

image upload and data submission in one form


joshgarrod

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.