SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Uploader Scripts Uploads Files Twice (Only Want Them Once)

    I've got 2 pages, one uploads the files and the second shows what files were uploaded. I've been told that what I'm doing is uploading the files twice. What would I strip out of my code so that it doesn't do that.
    Here is the code for the first page that allows you to upload the files:
    PHP Code:
    <?
    $num_of_uploads
    =$_POST['uploadNeed'];
    $file_types_array=array("jpg""JPG""pdf""PDF");
    $max_file_size=1048576;
    $upload_dir="";

    function 
    uploaderFILES($num_of_uploads=1$file_types_array=array("jpg""JPG""pdf""PDF"), $max_file_size=1048576$upload_dir=""){
      if(!
    is_numeric($max_file_size)){
       
    $max_file_size 1048576;
      }
      foreach(
    $_FILES["file"]["error"] as $key => $value)
      {
         if(
    $_FILES["file"]["name"][$key]!="")
         {
           if(
    $value==UPLOAD_ERR_OK)
           {
             
    $origfilename $_FILES["file"]["name"][$key];
             
    $filename explode("."$_FILES["file"]["name"][$key]);
             
    $filenameext $filename[count($filename)-1];
             unset(
    $filename[count($filename)-1]);
             
    $filename implode("."$filename);
             
    $filename substr($filename015).".".$filenameext;
             
    $file_ext_allow FALSE;
             for(
    $x=0;$x<count($file_types_array);$x++){
               if(
    $filenameext==$file_types_array[$x])
               {
                 
    $file_ext_allow TRUE;
               }
             } 
    // for
             
    if($file_ext_allow){
               if(
    $_FILES["file"]["size"][$key]<$max_file_size){
                 if(
    move_uploaded_file($_FILES["file"]["tmp_name"][$key], $upload_dir.$filename)){
                   echo(
    "File Uploaded Successfully. - <a href='".$upload_dir.$filename."' target='_blank'>".$filename."</a><br />");
                 } 
                 else { echo(
    '<font color="#FF0000">'.$origfilename."</font> Was Not Successfully Uploaded<br />");}
               }
               else  { echo(
    '<font color="#FF0000">'.$origfilename."</font> Was Too Big, Not Uploaded<br />"); }
             } 
    // if
             
    else{ echo('<font color="#FF0000">'.$origfilename." </font>Had An Invalid File Extension<br />");  }
           }
           else{ echo(
    '<font color="#FF0000">'.$origfilename." </font>Was Not Successfully Uploaded<br />");  } // else
         
    }
      }
    // funtion
    ?></td>
                    <td align="left" valign="top" class="Body">&nbsp;</td>
                  </tr>
                  <tr class="Body">
                    <td align="right" valign="top" class="Body">&nbsp;</td>
                    <td align="left" valign="top" class="Body"><?  for($x=0;$x<$num_of_uploads;$x++){
         
    $form .= "<input type='file' name='file[]'><br><br />";
       }
       
    $form .= "<input type='submit' value='Submit Information And Upload Files'><strong> Click Here When Ready.</strong><br><br />
       <font color='red'>*</font>Maximum file length (minus extension) is 15 characters. Anything over that will be cut to only 15 characters. Valid file type(s): "
    ;
       for(
    $x=0;$x<count($file_types_array);$x++){
         if(
    $x<count($file_types_array)-1){
           
    $form .= $file_types_array[$x].", ";
         }else{
           
    $form .= $file_types_array[$x].".";
         }
       }
       echo(
    $form);
    ?>
    And here is the code that shows what was uploaded:
    PHP Code:
    <?
    $num_of_uploads
    =$_POST['uploadNeed'];
    $file_types_array=array("jpg""JPG""pdf""PDF");
    $max_file_size=1048576;
    $upload_dir="";

    function 
    uploaderFILES($num_of_uploads=1$file_types_array=array("jpg""JPG""pdf""PDF"), $max_file_size=1048576$upload_dir=""){
      if(!
    is_numeric($max_file_size)){
       
    $max_file_size 1048576;
      }
      foreach(
    $_FILES["file"]["error"] as $key => $value)
      {
         if(
    $_FILES["file"]["name"][$key]!="")
         {
           if(
    $value==UPLOAD_ERR_OK)
           {
             
    $origfilename $_FILES["file"]["name"][$key];
             
    $filename explode("."$_FILES["file"]["name"][$key]);
             
    $filenameext $filename[count($filename)-1];
             unset(
    $filename[count($filename)-1]);
             
    $filename implode("."$filename);
             
    $filename substr($filename015).".".$filenameext;
             
    $file_ext_allow FALSE;
             for(
    $x=0;$x<count($file_types_array);$x++){
               if(
    $filenameext==$file_types_array[$x])
               {
                 
    $file_ext_allow TRUE;
               }
             } 
    // for
             
    if($file_ext_allow){
               if(
    $_FILES["file"]["size"][$key]<$max_file_size){
                 if(
    move_uploaded_file($_FILES["file"]["tmp_name"][$key], $upload_dir.$filename)){
                   echo(
    "File Uploaded Successfully. - <a href='".$upload_dir.$filename."' target='_blank'>".$filename."</a><br />");
                 } 
                 else { echo(
    '<font color="#FF0000">'.$origfilename."</font> Was Not Successfully Uploaded<br />");}
               }
               else  { echo(
    '<font color="#FF0000">'.$origfilename."</font> Was Too Big, Not Uploaded<br />"); }
             } 
    // if
             
    else{ echo('<font color="#FF0000">'.$origfilename." </font>Had An Invalid File Extension<br />");  }
           }
           else{ echo(
    '<font color="#FF0000">'.$origfilename." </font>Was Not Successfully Uploaded<br />");  } // else
         
    }
      }
    // funtion


    ?>
                    <?
    if(isset($_POST["submitted"])){
       
    uploaderFILES($num_of_uploads$file_types_array$max_file_size$upload_dir);
    }
    ?>

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kill the php logic in the first page to only display the form. The second page should deal with the actual upload logic. You can also have the form post to itself (through $_SERVER['PHP_SELF']) and check the isset($_POST['submitted']) logic as you have above.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •