472,119 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

multiple file upload to server and MySQL database

johngault
I've been working with this PHP page for several days now and I'm stumped. The page is supposed to allow the user to upload up to six images for their profile. When the user adds an image it (the page coding) does verify file size, if correct it places it on the server in the users folder and with the correct names and extension, creates the thumbnail and adds the correlating file name. However, it only adds the first file name to the database. When it displays the images we see the six columns in the table, but of course only the first column contains an image because the other image names did not get written into the database table. Does anyone see what is written wrong or missing? please!

Expand|Select|Wrap|Line Numbers
  1. <? if (!isset($_COOKIE["id"]) || $_COOKIE['usertype']!="chatmodels" )
  2.  
  3. {
  4.  
  5. header("location: ../../login.php");
  6.  
  7. } else{
  8.  
  9. include("../../dbase.php");
  10.  
  11. $result=mysql_query("SELECT user from $_COOKIE[usertype] WHERE id='$_COOKIE[id]' LIMIT 1");
  12.  
  13.  
  14.     while($row = mysql_fetch_array($result)) 
  15.  
  16.     {    $username=$row[user];    }
  17.  
  18. }
  19. mysql_free_result($result);
  20.  
  21. $errorMsg="You can upload up to 6 images";
  22.  
  23.  
  24.  
  25. //function that handles creation of thumbnails
  26.  
  27. function LoadJpeg ($imgname,$tocreate) {
  28.  
  29.     $tnsize="80";//thumbnail size
  30.  
  31.     $bigimage = @ImageCreateFromJPEG ($imgname); // Attempt to open 
  32.  
  33.     if (!$bigimage){
  34.  
  35.     $result=false;
  36.  
  37.     echo "<font color=#ffdd54>The image thumbnail could not be created. The image file might be corrupted.</font><br> You can can resave the file by using any image editor and then try again<br><br>Thank You! $endstr ";
  38.  
  39.     //exit();
  40.  
  41.     }
  42.  
  43.     $tnimage = ImageCreate($tnsize,$tnsize);
  44.  
  45.     $white = ImageColorAllocate ($tnimage,0, 0, 0);
  46.  
  47.     $sz = GetImageSize($imgname);
  48.  
  49.     // load our internal variables
  50.  
  51.     $x = $sz[0];    // big image width
  52.  
  53.     $y = $sz[1];    // big image height
  54.  
  55.  
  56.  
  57.     // find the larger dimension
  58.  
  59.         if ($x>$y) {    // if it is the width then
  60.  
  61.         $dx = 0;                    // the left side of the new image
  62.  
  63.         $w = $tnsize;                // the width of the new image
  64.  
  65.         $h = ($y / $x) * $tnsize;    // the height of the new image
  66.  
  67.         $dy = ($tnsize - $h) / 2;    // the top of the new image
  68.  
  69.         }else{    // if the height is larger then
  70.  
  71.         $dy = 0;                    // the top of the new image
  72.  
  73.         $h = $tnsize;                // the height of the new image
  74.  
  75.         $w = ($x / $y) * $tnsize;    // the width of the new image
  76.  
  77.         $dx = ($tnsize - $w) / 2;    // the left edgeof the new image
  78.  
  79.         }
  80.  
  81.     // copy the resized version into the thumbnal image
  82.  
  83.    ImageCopyResized($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h, $x, $y);
  84.  
  85.     //if we manage to create the thumbnail
  86.  
  87.    if (ImageJPEG($tnimage,$tocreate,80) && $x<640 && $y<640){
  88.  
  89.    $result=true;
  90.  
  91.    } else{ //if we dont
  92.  
  93.      $result=false;
  94.  
  95.               if ($x>640 || $y>640){
  96.  
  97.                $errorMsg="File resolution too big. Maximum 640x640 pictures accepted.";
  98.  
  99.               } else{
  100.  
  101.               $errorMsg="Thumbnail file could not be created";
  102.  
  103.               }
  104.  
  105.           //exit();
  106.  
  107.        }
  108.  
  109.   return $result;
  110.  
  111. }
  112.  
  113. if(!isset($_COOKIE["id"]))
  114.  
  115. {
  116.  
  117. header("Location: ../../login.php");
  118.  
  119. } else if (isset($_FILES['ImageFile']['tmp_name']))
  120.  
  121.     {    
  122.  
  123.         $currentTime=time();
  124.  
  125.         $pictureName=md5("$currentTime".$_SERVER['REMOTE_ADDR']);
  126.  
  127.  
  128.  
  129.         $urlImage="../../models/".$username."/".$pictureName.".jpg";
  130.  
  131.         $urlThumbnail="../../models/".$username."/".$pictureName."_thumbnail.jpg";
  132.  
  133.  
  134.  
  135.  
  136.  
  137.         //we copy the thumbail image
  138.  
  139.         if (copy ($_FILES['ImageFile']['tmp_name'],$urlImage) && LoadJpeg($urlImage,$urlThumbnail))
  140.  
  141.         {
  142.  
  143.         $id=$_COOKIE["id"];
  144.  
  145.         mysql_query("INSERT INTO modelpictures ( user , name, dateuploaded ) VALUES ('$username', '$pictureName', '$currentTime')");
  146.  
  147.         $errorMsg.='<img src="http://bytes.com/topic/models/'.$username.'/'.$pictureName.'_thumbnail.jpg"> File Copied';        
  148.  
  149.         }         
  150.  
  151.         else        
  152.  
  153.         {        
  154.  
  155.         $errorMsg.="File not Copied. Check resolution. Maximum 640kb files accepted.";        
  156.  
  157.         }
  158.  
  159.     } else  if(isset($_GET[delete]))
  160.  
  161.     {
  162.  
  163.     unlink("../../models/$username/$_GET[delete]_thumbnail.jpg");
  164.  
  165.     unlink("../../models/$username/$_GET[delete].jpg");
  166.  
  167.     mysql_query('DELETE from modelpictures WHERE name="'.$_GET[delete].'" LIMIT 1');
  168.  
  169.     $errorMsg+="File Deleted";    
  170.  
  171.     }
  172.  
  173.  
  174. ?>
  175.  
  176. <?
  177. include("_models.header.php");
  178. ?>
  179. <br>
  180.  
  181. <span style="font-weight: bold">This function is still under development process. Currently you can upload 1 image only!</span>
  182. <table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
  183.  
  184.   <tr valign="top">
  185.  
  186.     <td height="113"><form action="uploadpicture.php" method="post" enctype="multipart/form-data" name="form2">
  187.  
  188.         <p><span class="error">
  189.  
  190.           <?php if ( isset($errorMsg) && $errorMsg!=""){ echo $errorMsg; } ?>
  191.  
  192.         </span></p>
  193.  
  194.         <table width="720" border="0" align="center" cellpadding="4" cellspacing="0">
  195.  
  196.           <tr class="barbg">
  197.  
  198.             <td colspan="2"><span class="form_header_title">Upload new Image </span></td>
  199.           </tr>
  200.  
  201.           <tr align="right">
  202.  
  203.             <td width="555" align="left"><input name="ImageFile" type="file" id="ImageFile">
  204.  
  205.                 <input type="submit" name="Submit2" value="Upload image to my gallery"></td>
  206.  
  207.           </tr>
  208.  
  209.         </table>
  210.  
  211.         <br>
  212.  
  213.         <table width="720" border="0" align="center" cellpadding="4" cellspacing="0">
  214.  
  215.           <tr class="barbg">
  216.  
  217.             <td class="barbg"><span class="form_header_title">Image  Gallery </span></td>
  218.  
  219.           </tr>
  220.  
  221.           <tr>
  222.  
  223.             <td>
  224.  
  225.               <table width="700" border="1" align="center" bordercolor="#CCCCCC">
  226.  
  227.                 <?
  228.  
  229.             $count=0;
  230.  
  231.             $result = mysql_query('SELECT * FROM modelpictures WHERE user="'.$username.'" ORDER BY dateuploaded DESC');
  232.  
  233.             while($row = mysql_fetch_array($result)) 
  234.  
  235.             {
  236.  
  237.             $count++;
  238.  
  239.             if ($count>0) {echo"<tr>";}
  240.  
  241.             echo "<td width='100'class='form_definitions' height='100' align='center' valign='middle'><img src ='../../models/".$username."/".$row[name]."_thumbnail.jpg' ><br><a href='uploadpicture.php?delete=$row[name]'>Delete</a></td>";
  242.  
  243.             if ($count==6){ echo"</tr>"; $count=0;}
  244.  
  245.             }
  246.  
  247.             mysql_free_result($result);
  248.  
  249.             for($i=0; $i<6-$count; $i++)
  250.  
  251.             {
  252.  
  253.             echo"<td width='100' height='100' align='center' valign='middle'>&nbsp</td>";
  254.  
  255.             }
  256.  
  257.             echo"</tr>";
  258.  
  259. ?>
  260.  
  261.             </table></td>
  262.  
  263.           </tr>
  264.  
  265.         </table>
  266.  
  267.         </form></td>
  268.  
  269.   </tr>
  270.  
  271.   <tr>
  272.  
  273.     <td>&nbsp;</td>
  274.  
  275.   </tr>
  276.  
  277. </table>
Dec 30 '08 #1
8 9654
Atli
5,058 Expert 4TB
Hi.

On line #145. You execute the query to add the new image to the database, but there is nothing in place to check whether the row was actually added. All errors that could be occurring would simply be ignored.

Make sure that query is in fact working before you do anything else.
(See Turn on PHP Debugging Messages to see how to do that)
Dec 30 '08 #2
Thank you for your patience.
I added the error checking and found a warning on line 241, undefined constant assumed 'name'.
So I added $name=$row["name"]; in line 234 and changed line 241 to:
Expand|Select|Wrap|Line Numbers
  1. echo "<td width='100'class='form_definitions' height='100' align='center' valign='middle'><img src ='../../models/".$username."/".$name."_thumbnail.jpg' ><br><a href='uploadpicture.php?delete=$name'>Delete</a></td>";
I see no further warnings or errors. I can upload 1 image (file name) successfully to the sql table and display that 1 image. Uploading additional image files will add to the server folder, but not the sql table.
Dec 30 '08 #3
Atli
5,058 Expert 4TB
And did the query in line #145 get executed successfully every time?

To make sure of that, try something like:
Expand|Select|Wrap|Line Numbers
  1. $sql = "INSERT INTO ...";
  2. $result = mysql_query($sql) 
  3.   or die("<h3>Query failed!</h3><pre>". mysql_error() ."</pre>");
  4.  
  5. if(mysql_get_affected_rows($result) != 1) {
  6.   die("<h3>Failed to insert!</h3><pre>". mysql_error() ."</pre>");
  7. }
That should tell you why the row isn't getting inserted.
Dec 30 '08 #4
Adding/changing line 145 with the above resulted in:
Query failed!
Duplicate entry 'Username' for key 1
Dec 30 '08 #5
Since the primary key in the Sql table is the Username, the subsequent images beyond the first do not get added to the table because Sql will not allow more than one Unique entry to the primary key. Nice what reading errors and warnings can trigger in the grey matter! :) So -- by adding a check at the beginning of the process to verify if the Username already exists in the table and if not create it and than removing the insert 'user' from line 145 allows Sql to do what we expect it to do.
Dec 31 '08 #6
Atli
5,058 Expert 4TB
Ahh ok. So the username is the primary key.
Then there is no good way to add multiple pictures per user, using your current table structure.
If you simply remove the 'user' field from the INSERT clause, then you will just have a bunch of images floating around without a user.

But, if you add a integer field to your current structure and make that your primary key, you can use your code just as it is.

I mean, if you table looked somewhat like:
Expand|Select|Wrap|Line Numbers
  1. modelpicture
  2. ----------------
  3. id int unsigned primary key
  4. user varchar not null
  5. image varchar not null
  6. created datetime not null
  7.  
The 'id' field would act as a row counter of sorts, just what a PK is usually supposed to be.
Dec 31 '08 #7
Perfect. That solution was right on. I had to make the id primary key auto increment to get past the duplicate value error from Sql.
Thank you for your help. Awesome!
Dec 31 '08 #8
Atli
5,058 Expert 4TB
I'm glad I was able to help :)
Dec 31 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by shahram.shirazi | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.