SunQuest
 
           General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old October 16th, 2002, 12:37 PM
elemental elemental is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 1 elemental User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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_resizedImageCreateFromJpeg($_FILES['strFile']['tmp_name']), 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. . .

Reply With Quote
  #2  
Old October 16th, 2002, 04:59 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
First of all, when you're posting that much code, make sure it's documented for faster reading.

Second, you should be using 1 table to store an ID, date, title, content, and another table to store an ID and the binary image data. That way you can upload whatever images are there as seperate entries in a table, and you can pull them using a join on the ID.

Thirdly, you should try to do a bit more in the way of sanity checks on your $_REQUEST data. I also suggest using $_GET and $_POST in case you have mixed request data for readability and logic sake. But you need to do, at the very least, a strip_tags() and add_slashes() on your title and content uploads to be sure you're not letting users submit malicious code that could, for instance, drop your entire database.

Finally, seperate your code into functions. Especially the resizing of the image section. Try to have them return either true/false or one string containing all your formatted output. It makes your code a LOT more readable. I like to include my functions from another file, and i also like to include a header/footer file that contains your html headers , body opening tags/body and html closing tags. Using one for your form is also helpful. The more modular it is, the more portable and editable it is, and the easier it will be to troubleshoot it.

Sorry I don't have a specific solution but I don't like reading through undocumented code with no functions =) Hope the above info helps.

Reply With Quote
  #3  
Old October 16th, 2002, 05:02 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
one other thing, i noticed $_REQUEST['PHP_SELF'] in your form action="" tag. just use $PHP_SELF. and is your code writing ANYTHING to the database? and, are you putting the binary data into the database? i would highly suggest it.

Reply With Quote
  #4  
Old October 16th, 2002, 07:55 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Whoa. major problem!

your trying to upload an "array" of files, yet, your code doesnt handle strFile as an array!

you will need to use the foreach function to upload the files in the array.

Also make sure you dont post so much code, it really puts people off from helping you.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Help With Multiple Image Uploads


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway