
October 16th, 2002, 12:37 PM
|
|
Junior Member
|
|
Join Date: Oct 2002
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Help With Multiple Image Uploads
Hey All,
I've been working on this script for a couple days now with no real success. Basically, I just want to have a form that allows users to upload more than one image for an article. Below is the code that I am using. Basically, I'm not getting any errors, but no files are being uploaded. If anybody can spot any problems or give me some guidance either with this script or another way of doing it, I would really appreciate it. . .
Here is the PHP part:
PHP Code:
include ("../includes/inc-db.php");
if ($_REQUEST['submit']): //User Has Added Category
if ($_REQUEST['aid'] == "") {
echo("<p>You must choose an author " .
"for this story. Click 'Back' " .
"and try again.</p>");
exit();
}
$dbcnx = mysql_connect($host, $user, $password);
mysql_select_db($db);
//Define Variables
$name = $_REQUEST['name'];
$headline = $_REQUEST['headline'];
$story = $_REQUEST['story'];
$aid = $_REQUEST['aid'];
$sql = "INSERT INTO stories SET
headline='$headline',
story='$story',
create_date=CURDATE(),
aid='$aid'";
if (mysql_query($sql)) {
echo("<p>Story has been added</p>");
}
else {
echo ("<p>Error Adding Story</p>");
}
$sid = mysql_insert_id();
if ($_REQUEST['cats'] == "") $cats = array();
$cats = $_REQUEST['cats'];
foreach ($cats as $catID) {
$add = "INSERT INTO storylookup
SET SID=$sid, CID=$catID";
$ok = mysql_query($add);
if ($ok) {
$numCats = $numCats + 1;
} else {
echo("<p>Error inserting story into category $catID: " .
mysql_error() . "</p>");
}
}
echo "<p>Story was added to $numCats categories.</p>";
$strFile = $_REQUEST['strFile'];
foreach ($strFile as $imgID) {
$report_img = '';
$IMG_WIDTH = 275;
$IMG_HEIGHT = 275;
$IMG_ROOT = "../uploads";
$fileName = explode(".", $imgID['name']);
$name = str_replace(' ', '_', $fileName[0]);
error_reporting(53); //errors may happen for no reason, so this killes them, we will use our own error reporting
$acceptedTypes = array('image/jpg');
if(!$_FILES['strFile']['type'] == "image/jpg" || trim($_FILES['strFile']['tmp_name']) == "" || trim($_FILES['strFile']['tmp_name']) =="none")
{
echo 'You didnt suply an image a valid image.';
}
else
{
$img_orig_size = getimagesize($_FILES['strFile']['tmp_name']);
$img_orig_width = $img_orig_size[0];
$img_orig_height = $img_orig_size[1];
$img_original = ImageCreateFromJpeg($_FILES['strFile']['tmp_name']);
$image_stored = time() . '_' . "_$name.jpg";
ImageJPEG($img_original, "$IMG_ROOT/$image_stored");
echo "Image Name: {$imgID['name']}<br>
New Image Name: $image_stored<br>
Width: $img_orig_width<br>
Height: $img_orig_height<br>
Max Image Size for Thumbnails: $IMG_WIDTH x $IMG_HEIGHT<br>
<a target='_blank' href='$IMG_ROOT/$image_stored'>View Original Image</a><br><br>";
//the image will need to be resized, as its too big. It will be a thumbnail
$mlt_w = $IMG_WIDTH / $img_orig_width;
$mlt_h = $IMG_HEIGHT / $img_orig_height;
$mlt = $mlt_w < $mlt_h ? $mlt_w:$mlt_h;
#### Calculate new dimensions
$img_new_width = round($img_orig_width * $mlt);
$img_new_height = round($img_orig_height * $mlt);
$img_resized = ImageCreate($img_new_width, $img_new_height);
imagecopyresized($img_resized, ImageCreateFromJpeg($_FILES['strFile']['tmp_name']), 0 , 0 , 0 , 0, $img_new_width, $img_new_height, $img_orig_width, $img_orig_height);
$img_name = "tn_$image_stored";
Imagejpeg($img_resized, "$IMG_ROOT/$img_name");
ImageDestroy($img_resized);
$mg_new_size = filesize("$IMG_ROOT/$img_name");
echo "<b>Image Resized</b><br>
Width: $img_new_width<br>
Height: $img_new_height<br>
FileSize: $mg_new_size (in bytes)<br>
";
}
$addImg = "INSERT INTO images
SET SID=$sid, image=$image_stored, thumb=$img_name";
}
And here is the form part:
Quote: <form action="<?php $_REQUEST['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<p>Enter You Story:</p>
<table width="75%">
<tr>
<td width="25%">Headline:</td>
<td><input name="headline" type="text" id="headline" size="50" /></td>
</tr>
<tr>
<td width="25%">Story:</td>
<td><textarea name="story" cols="50" rows="10" id="story"></textarea></td>
</tr>
<tr>
<td width="25%">Photo 1: </td>
<td><input type="file" name="strFile[]" /></td>
</tr>
<tr>
<td width="25%">Photo 2: </td>
<td><input type="file" name="strFile[]" /></td>
</tr>
<tr>
<td width="25%">Photo 3: </td>
<td><input type="file" name="strFile[]" /></td>
</tr>
<tr>
<td width="25%">Photo 4: </td>
<td><input type="file" name="strFile[]" /></td>
</tr>
<tr>
<td width="25%">Photo 5: </td>
<td><input type="file" name="strFile[]" /></td>
</tr>
<tr>
<td>Author:</td>
<td>
<select name="aid" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>
<?php
while ($author = mysql_fetch_array($authors)) {
$aid = $author["id"];
$aname = htmlspecialchars($author["fname"]);
echo("<option value='$aid'>$aname</option>\n");
}
?>
</select>
</td>
</tr>
<tr>
<td valign="top">Category:</td>
<td>
<?php
while ($cat = mysql_fetch_array($cats)) {
$cid = $cat["id"];
$cname = htmlspecialchars($cat["name"]);
echo("<input type='checkbox' name='cats[]' value='$cid'>$cname<br />\n");
}
?>
</td>
</tr>
</table>
<p>
<input name="submit" type="submit" id="submit" value="Submit Story" />
</p>
</form> |
Any help would be appreciated. . .
|