A Digital Age Deserves A Digital Leader

PHP web app help

Postby jbullard » Mon May 12, 2008 4:26 pm

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. :)
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Postby DRAGON OF DARKNESS » Tue May 13, 2008 1:23 pm

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 :)
Stephen
PRO Level 16
User avatar
Posts: 1826
Joined: Fri Jul 16, 2004 11:14 pm
Location: MIA > FLA > USA

Postby jbullard » Tue May 13, 2008 5:49 pm

This is somewhat how I would do it.

Code: Select all
<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>
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Postby DRAGON OF DARKNESS » Wed May 14, 2008 1:49 pm

Thanks jason ill try it out :)
Stephen
PRO Level 16
User avatar
Posts: 1826
Joined: Fri Jul 16, 2004 11:14 pm
Location: MIA > FLA > USA

Postby DRAGON OF DARKNESS » Fri May 16, 2008 2:27 am

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 :)
Stephen
PRO Level 16
User avatar
Posts: 1826
Joined: Fri Jul 16, 2004 11:14 pm
Location: MIA > FLA > USA

Postby jbullard » Fri May 16, 2008 3:33 am

Not a problem man and glad to hear you got it working. :yesnod:
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Previous

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: No registered users and 3 guests

cron
cron