PDA

View Full Version : uploading an image and inputting text then writing to a file with forms


jakobmetzger
12-14-2006, 07:50 PM
Ok, I want this to upload and image, then get "Title" and "Description" to be written to a page (forsale.php) and have the image in there also. Ive gotten the title and description part down but I dont know how to go about the image upload part.
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<FORM METHOD="POST" enctype="multipart/form-data" ACTION="addproduct.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td rowspan="2"><div align="center">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" /></div></td>
<td><input name="title" type="text" value="Title" /></td>
</tr>
<tr>
<td><div align="left"><textarea name="description" cols="40" rows="10">Description of Product
</textarea>
</div></td>
</tr>
</table>
<INPUT TYPE="submit">
</FORM>

</body>
</html>

<?php
$myFileread = "forsale.php";
$fileread = fopen($myFileread, 'r');
$oldstuff = fread($fileread, filesize($myFileread));

$myFile = "forsale.php";
$fh = fopen($myFile, 'w') or die("can't open file");



$image = $_POST['image'];
$title = $_POST['title'];
$description = $_POST['description'];

$html = "<table width=100% align=center border=0 cellspacing=0 cellpadding=0><tr><td rowspan=2 width=50%>&nbsp;</td><td>$title</td></tr><tr><td>$description</td></tr></table><br><br>$oldstuff";

echo "Title -". $title .".<br /> Description -" . $description . ".<br />";
echo "You have just added a product to your For Sale page!";

fwrite($fh, $html);
fclose($fh);
fclose($fileread);

?>