كود:
<?php
include("../includes/meta.php");
require_once("../includes/connection.php");
$action = $_GET['add'];
$max_no_img = 10; // Maximum number of images value to be set here
function createThumbs( $pathToImages, $pathToThumbs, $fname, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
$fname = readdir( $dir );
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
//echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
// close the directory
closedir( $dir );
}
?>
<title>add product</title>
</head>
<body>
<div id="container">
<?php
if($action == "ok"){
$title = trim($_POST['ProductName']);
$number = trim($_POST['ProductNumber']);
$desc = trim($_POST['ProductDescription']);
$Price = floatval($_POST['ProductPrice']);
$WasPrice = floatval($_POST['ProductWasPrice']);
if ($title == ""){
echo "<br /><br /><p style=\"color: red;\">You must enter a Product Name</p><br /><br />";
} else if ($number == ""){
echo "<br /><br /><p style=\"color: red;\">You must enter a Product Number</p><br /><br />";
} else if ($desc == ""){
echo "<br /><br /><p style=\"color: red;\">You must enter a Product Description</p><br /><br />";
} else if (empty($Price)){
echo "<br /><br /><p style=\"color: red;\">You must enter a Product Price</p><br /><br />";
} else if ($Price >= $WasPrice){
echo "<br /><br /><p style=\"color: red;\">Product Price must be greater that Product Was Price</p><br /><br />";
} else {
$images = "";
while(list($key,$value) = each($_FILES[images][name]))
{
if(!empty($value)){ // this will check if any blank field is entered
$filename = $value; // filename stores the value
$NewName = $number;
$NewName .= substr($filename, 0, strrpos( $filename ,"."));
$NewName = str_replace(" ","-", $NewName); // Add - inplace of blank space in file name, you can remove this line
$NewName .= rand(100, 1000000);
$NewName .= substr($filename, strrpos( $filename ,".") );
$images .= $NewName;
$images .= ",";
$add = "../Products/$NewName"; // upload directory path is set
copy($_FILES[images][tmp_name][$key], $add); // upload the file to the server
sleep(3);
createThumbs( "../Products/", "../Products/Thumbs/", $NewName , "180" );
}
}
$images = substr($images, 0, -1);
$sql_add = mysql_query("INSERT INTO Products (Title, Description, Pictures, ItemNum, ProductPrice, ProductWasPrice) VALUES ('$title' , '$desc' , '$images' , '$number' , '$Price' , '$WasPrice') ");
if($sql_add){
echo "<br /><br /><p style=\"color: green;\">A new product has been added</p><br /><br />";
} else {
echo "<br /><br /><p style=\"color: red;\">Error occurred while trying to add new product to the database</p><br /><br />";
}
}
echo "</div>
</body>
</html>";
exit();
}
?>
<form method="post" action="Products-Add.php?add=ok" enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-style: solid; border-width: 1px; border-color: #333333;" width="770">
<tr>
<td style="text-align: left; border: 1px #333333; " width="30%">Product Name:</td>
<td style="text-align: left; border: 1px #333333; " width="70%"><input type="text" name="ProductName" size="50" /></td>
</tr>
<tr>
<td style="text-align: left; border: 1px #333333; " width="30%">Product Number:</td>
<td style="text-align: left; border: 1px #333333; " width="70%"><input type="text" name="ProductNumber" size="7" /></td>
</tr>
<tr>
<td style="text-align: left; border: 1px #333333; " width="30%">Product Price:</td>
<td style="text-align: left; border: 1px #333333; " width="70%"><input type="text" name="ProductPrice" size="7" /></td>
</tr>
<tr>
<td style="text-align: left; border: 1px #333333; " width="30%">Product Was Price:</td>
<td style="text-align: left; border: 1px #333333; " width="70%"><input type="text" name="ProductWasPrice" size="7" /></td>
</tr>
<tr>
<td style="text-align: left; border: 1px #333333; " width="30%">Product Description:</td>
<td style="text-align: left; border: 1px #333333; " width="70%"><textarea name="ProductDescription" cols="60" rows="10"></textarea></td>
</tr>
<tr>
<td style="text-align: left; border: 1px #333333; " width="30%">Product Pictures:</td>
<td style="text-align: left; border: 1px #333333; vertical-align: top;" width="70%">
<?php
echo "<table cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse; border-style: dotted; border-width: 1px; border-color: #333333;\" width=\"95%\">
<tr><td>Num</td><td>Main Pic</td><td>Select an Image<td></tr>";
for($i=1; $i<= $max_no_img; $i++){
if ($i == 1) {
$SELECTED = " checked ";
} else {
$SELECTED = "";
}
echo "<tr><td style=\"border: 1px dotted #333333;\">Image $i</td><td style=\"border: 1px dotted #333333;\"><input type=\"radio\" name=\"MainPic[]\" $SELECTED /></td><td style=\"border: 1px dotted #333333;\"> <input type='file' name='images[]' class='bginput' /></td></tr>";
}
echo "</table>";
?>
</td>
</tr>
</table>
<br />
<input type="submit" value="Add Product" />
</form>
</div>
</body>
</html>