Attention: PROnetworks has upgraded our forum from phpbb2 to phpbb3!!

Please head over to our new converted forum at: http://www.pronetworks.org/forums/

This old forum will remain 'read-only' until approximately February 2009. We look forward to seeing you at the new forum!
Author Message
jbullard
Jason Bullard
PostPosted: Mon May 12, 2008 12:26 pm Reply with quote

VP - Software
 
 


Joined: 06 Jun 2004
Posts: 3304
Location: Utah
It really depends on how you are inserting data into the DB. If each file is considered a new row than you would need to get the users id (or however you track each user) and then just insert the file each loop.

Or, if you were to add them into a serialized array than you would just add each file to an array, serialize it, and then update the users information.

If you could post how your DB is setup for this it would help out as there are many ways of doing this. smile
 
Back to top
DRAGON OF DARKNESS
PostPosted: Tue May 13, 2008 9:23 am Reply with quote

PRO SILVER
 
 


Joined: 16 Jul 2004
Posts: 4674
Location: MIA > FLA > USA
Ok heres what im doing exactly, Im input just the links into the same row as all the other data in the form. Theres to fields on the end labeled download1 and download2. So basicly i need it to save the file name and path and ill parse the domain in myself. I just dont know how to get it to send the link to each of the different fields in the array. Thanks smile
 
Back to top
jbullard
Jason Bullard
PostPosted: Tue May 13, 2008 1:49 pm Reply with quote

VP - Software
 
 


Joined: 06 Jun 2004
Posts: 3304
Location: Utah
This is somewhat how I would do it.

Code:

<html>
   <head>
   <title>Secure PHP File Upload</title>
   </head>
   
   <body>

<?php

// Check if form was submitted
if (isset($_POST['Submit']))
{
   // Create variable to count
   $i = 1;
   
   // Create variable to continue
   $pass = FALSE;
   
   // Setup sql command
   $command = array(
                  'firstname'      =>   $_POST['firstname'],
                  'lastname'      =>   $_POST['lastname'],
                  'dob'         =>   $_POST['dob'],
                  'dr'         =>   $_POST['dr'],
                  'startdate'      =>   $_POST['startdate'],
                  'starttime'      =>   $_POST['starttime'],
                  'indications'   =>   $_POST['indications'],
                  'FILE1'         =>   '',
                  'FILE2'         =>   ''
            );
   
   // Loop through files
   foreach ($_FILES['download']['error'] as $key => $val)
   {
      // Check upload status
      if ($val == UPLOAD_ERR_OK)
      {
         // Get file name
         $filename = basename($_FILES['download']['name'][$key]);
         
         // Get extension
         $ext = substr($filename, strrpos($filename, '.') + 1);
         
         // Only allow text files to be uploaded under 350Kb
         if (($ext == "txt") && ($_FILES['download']['size'][$key] < 350000))
         {
            // Create new path name
            $newname = dirname(__FILE__).'/uploads/'.$filename;
             
            // Check if file already exists
            if (!file_exists($newname))
            {
               // Move uploaded file to new path
               if ((move_uploaded_file($_FILES['download']['tmp_name'][$key], $newname)))
               {
                  // File uploaded okay
                  echo "File uploaded successfully.<p />";
                 
                  echo $_POST['firstname']."<br />";
                  echo $_POST['lastname']."<br />";
             
               // Add file to command
               $command['FILE'.$i] = $newname;
               
               // Set pass
               $pass = TRUE;
               }
               else
               {
                  // There was an upload error
                  echo "Error:  A problem occured during file upload.<p />";
             
              // Set pass
              $pass = FALSE;
               }
            }
            else
            {
               // File already existed
               echo "Error: File ".$_FILES['download']['name'][$key]." already exists.<p />";
            
            // Set pass
            $pass = FALSE;
            }
         }
         else
         {
            // Only text files under 350Kb
            echo "Error:  Only .txt files under 350Kb are authorized for upload.".$_FILES['download']['size'][$key]."<p />";
         
         // Set pass
         $pass = FALSE;
         }
      }
      else
      {
         // No file uploaded (Error)
         echo "Error: No file uploaded.<p />";
      }
      
      // Increment $i
      $i++;
   }
   
   if ($pass == TRUE)
   {
      // Generate mysql inserts
      foreach ($command as $key => $value)
      {
         $fields = $fields . ', ' .$key;
         $values = $values . ', ' . $value;
      }
      
      // Execute query
      $sql = mysql_query("INSERT INTO `TABLE_NAME` ($fields) VALUES($values)");
   }
}
?>
      <form enctype="multipart/form-data" action="index.php" method="post">
         Firstname: <input type="text" name="firstname" value="" />
         <br />
         Lastname: <input type="text" name="lastname" value="" />
         <br />
         DOB:  <input type="text" name="dob" value="" />
         <br />
         Doctor: <input type="text" name="doctor" value="" />
         <br />
         Start Date: <input type="text" name="start_date" value="" />
         <br />
         Start Time: <input type="text" name="start_time" value="" />
         <br />
         Indications: <input type="text" name="indications" value="" />
         <br />
         <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
         Download1: <input type="file" name="download[]" value="" />
         <br />
         Download2: <input type="file" name="download[]" value="" />
         <p />
         <input type="submit" name="Submit" value="Submit" />
      </form>
   </body>
</html>
 
Back to top
DRAGON OF DARKNESS
PostPosted: Wed May 14, 2008 9:49 am Reply with quote

PRO SILVER
 
 


Joined: 16 Jul 2004
Posts: 4674
Location: MIA > FLA > USA
Thanks jason ill try it out smile
 
Back to top
DRAGON OF DARKNESS
PostPosted: Thu May 15, 2008 10:27 pm Reply with quote

PRO SILVER
 
 


Joined: 16 Jul 2004
Posts: 4674
Location: MIA > FLA > USA
Ok i sort of used your concept and made up my own thing xD but hey it worked ! Thanks for all your help ! I think im done here smile
 
Back to top
jbullard
Jason Bullard
PostPosted: Thu May 15, 2008 11:33 pm Reply with quote

VP - Software
 
 


Joined: 06 Jun 2004
Posts: 3304
Location: Utah
Not a problem man and glad to hear you got it working. smilenod
 
Back to top
Back to top
Index >> Webmaster Domain & Code Room >> PHP web app help

Goto page Previous  1, 2, 3

Page 3 of 3

 


Tired of the Ads? Registered users have 80% less adverts.