Passing Filename Variable To mySql Database

Last post 02-08-2012, 10:49 AM by IRHM73. 2 replies.
Sort Posts: Previous Next
  •  02-04-2012, 12:55 PM 72921

    Passing Filename Variable To mySql Database

    I wonder whether someone may be able to help me please.
     
    I've put together the following code which allows the user to select and manually upload files, saving it to a folder on my server which all works fine.
    1. <?php require_once "phpuploader/include_phpuploader.php" ?>   
    2. <?php session_start(); ?>   
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">   
    4. <html xmlns="http://www.w3.org/1999/xhtml">   
    5. <head>   
    6.     <title>   
    7.         Form - Start uploading manually   
    8.     </title>   
    9.     <link href="demo.css" rel="stylesheet" type="text/css" />   
    10.                
    11.     <script type="text/javascript">   
    12.     function doStart()   
    13.     {   
    14.         var uploadobj = document.getElementById('myuploader');   
    15.         if (uploadobj.getqueuecount() > 0)   
    16.         {   
    17.             uploadobj.startupload();   
    18.         }   
    19.         else  
    20.         {   
    21.             alert("Please browse files for upload");   
    22.         }   
    23.     }   
    24.     </script>   
    25.        
    26. </head>   
    27. <body>   
    28.     <div class="demo">        
    29.             <h2>Start uploading manually</h2>   
    30.             <p>This sample demonstrates how to start uploading manually after file selection vs automatically.</p>   
    31.             <P>Allowed file types: <span style="color:red">jpg, gif, txt, png, zip</span></p>   
    32.   
    33.             <!-- do not need enctype="multipart/form-data" -->   
    34.             <form id="form1" method="POST">   
    35.                 <?php                   
    36.                     $uploader=new PhpUploader();   
    37.                     $uploader->MaxSizeKB=10240;   
    38.                     $uploader->Name="myuploader";   
    39.                     $uploader->InsertText="Select multiple files (Max 10M)";   
    40.                     $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.txt,*.zip,*.rar";     
    41.                     $uploader->MultipleFilesUpload=true;   
    42.                     $uploader->ManualStartUpload=true;   
    43.                     $uploader->Render();   
    44.                 ?>   
    45.                 <br /><br /><br />   
    46.                 <button id="submitbutton" onclick="doStart();return false;">Start Uploading Files</button>   
    47.   
    48.                
    49.             <br/><br/><br/>   
    50. <?php   
    51.   
    52. $files=array();   
    53.   
    54. $processedlist=@$_POST["processedlist"];   
    55. if($processedlist)   
    56. {   
    57.     $guidlist=explode("/",$processedlist);   
    58.     foreach($guidlist as $fileguid)   
    59.     {   
    60.         $mvcfile=$uploader->GetUploadedFile($fileguid);   
    61.         if($mvcfile)   
    62.         {   
    63.             array_push($files,$mvcfile);   
    64.         }   
    65.     }   
    66. }   
    67. $fileguidlist=@$_POST["myuploader"];   
    68. if($fileguidlist)   
    69. {   
    70.     $guidlist=explode("/",$fileguidlist);   
    71.     foreach($guidlist as $fileguid)   
    72.     {   
    73.         $mvcfile=$uploader->GetUploadedFile($fileguid);   
    74.         if($mvcfile)   
    75.         {   
    76.             //Process the file here..   
    77.             //rename(..)   
    78.                
    79.             if($processedlist)   
    80.                 $processedlist$processedlist . "/" . $fileguid;   
    81.             else  
    82.                 $processedlist$fileguid;   
    83.            
    84.             array_push($files,$mvcfile);   
    85.         }   
    86.     }   
    87. }   
    88.   
    89. if(count($files)>0)   
    90. {   
    91.     echo("<table style='border-collapse: collapse' class='Grid' border='0' cellspacing='0' cellpadding='2'>");   
    92.     foreach($files as $mvcfile)   
    93.     {   
    94.         echo("<tr>");   
    95.         echo("<td>");echo("<img src='phpuploader/resources/circle.png' border='0' />");echo("</td>");   
    96.         echo("<td>");echo($mvcfile->FileName);echo("</td>");   
    97.         //echo("<td>");echo($mvcfile->FileSize);echo("</td>");   
    98.         echo("</tr>");   
    99.   
    100.         //Moves the uploaded file to a new location.   
    101.         //$mvcfile->MoveTo("/uploads");   
    102.         //Copys the uploaded file to a new location.   
    103.         $mvcfile->CopyTo("savefiles");   
    104.         //Deletes this instance.   
    105.         //$mvcfile->Delete();   
    106.     }   
    107.     echo("</table>");   
    108. }   
    109.   
    110. ?>   
    111.   
    112.                 <input type='hidden' name='processedlist' value='<?php echo($processedlist) ?>' />   
    113.   
    114.                 <br /><br />   
    115.                 <input type='submit' value="Submit Form" />   
    116.                
    117.             </form>      
    118.     </div>   
    119. </body>   
    120. </html>  
    What I'd now like to do is pass the filename variable to a mySql database. I've read the various posts on this forum and realised, or so I thought that the variable I need to capture is '$mvcfile'.
     
    In the final verison of my form there will be a number of form fields besides the 'filename' variable I need to capture so I've put together the following php script to save the data.
    1. <?php   
    2.   
    3. require("phpfile.php");   
    4. // Gets data from form   
    5.   
    6. // Opens a connection to a MySQL server   
    7. $conn = mysql_connect("hostname"$username$password);   
    8. if (!$conn)   
    9. {   
    10.   die('Not connected : '.mysql_error());   
    11. }   
    12.   
    13. // Set the active MySQL database   
    14. $db_selected = mysql_select_db($database$conn);   
    15. if (!$db_selected)   
    16. {   
    17.   die('Can\'t use db : '.mysql_error());   
    18. }   
    19.   
    20. $query = "INSERT INTO testimages(imagename) VALUES ('$mvcfile')";    
    21. $result = mysql_query($query$conn);   
    22.   
    23. if (!$result)   
    24. {   
    25.   die('Invalid query: '.mysql_error());   
    26. }   
    27.   
    28. ?>  
    The problem is, is that I can't get this to pull the data from the variable and into my database. I've tried all different naming conventions without any luck. I just wondered whether someone could perhaps have a look at this please and let me know where I'm going wrong, or whether, indeed there's a simpler within the 'PHPFileUploader' software that allows you to do this.
     
    Many thanks and regards
     
    Chris
    Filed under:
  •  02-06-2012, 12:03 AM 72927 in reply to 72921

    Re: Passing Filename Variable To mySql Database

    Hi IRHM73,
     
    If you code  line 96 is the method to get the upload file name. If it output the file name in the page correct. It means that it gets the correct name.
     
    $mvcfile->FileName
     
    Regards,
     
    Ken 
  •  02-08-2012, 10:49 AM 72958 in reply to 72927

    Re: Passing Filename Variable To mySql Database

    Hi Ken, sincere thanks for your reply.
     
    After your suggestion I went and re-wrote the code and in case anyone needs a helping hand on sending the form data to mySql databases, this is what I've put together.
     

    <?php require_once "phpuploader/include_phpuploader.php" ?>
    <?php session_start(); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <title>
      Form - Keeping state after submitting
     </title>
     <link href="demo.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
     <div class="demo">
       
       <h2>Keeping state after submitting</h2>
       <p>A sample demonstrates how to keep uploaded file state during page postbacks.</p>

       <!-- do not need enctype="multipart/form-data" -->
       <form id="form1" method="POST">
        <?php

        $uploader=new PhpUploader();
        $uploader->MaxSizeKB=10240;
        $uploader->MultipleFilesUpload=true;
        $uploader->Name="myuploader";
        $uploader->InsertText="Select multiple files (Max 10M)";
        $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.bmp,*.txt,*.zip,*.rar"; 
        $uploader->Render();

        ?>
        
        <br/><br/><br/>
        
    <?php

    $files=array();

    $processedlist=@$_POST["processedlist"];
    if($processedlist)
    {
     $guidlist=explode("/",$processedlist);
     foreach($guidlist as $fileguid)
     {
      $mvcfile=$uploader->GetUploadedFile($fileguid);
      if($mvcfile)
      {
       array_push($files,$mvcfile);
      }
     }
    }
    $fileguidlist=@$_POST["myuploader"];
    if($fileguidlist)
    {
     $guidlist=explode("/",$fileguidlist);
     foreach($guidlist as $fileguid)
     {
      $mvcfile=$uploader->GetUploadedFile($fileguid);
      if($mvcfile)
      {
       
            $connect = @mysql_connect("hostname", "username", "password");
          if ( ! $connect ) {
        die( "Couldn't connect to MySQL: ".mysql_error() );
       }
       $db = "database";
       @mysql_select_db( $db ) or die ( "Couldn't open $db: ".mysql_error() );
          $sql="insert into testimages (imagename) values('$mvcfile->FileName')";
      
          mysql_query( $sql, $connect ) or die ( "INSERT error: ".mysql_error() );     
          mysql_close($connect);
       
       if($processedlist)
        $processedlist= $processedlist . "/" . $fileguid;
       else
        $processedlist= $fileguid;
      
       array_push($files,$mvcfile);
      }
     }
    }

    if(count($files)>0)
    {
     echo("<table style='border-collapse: collapse' class='Grid' border='0' cellspacing='0' cellpadding='2'>");
     foreach($files as $mvcfile)
     {
      echo("<tr>");
      echo("<td>");echo("<img src='phpuploader/resources/circle.png' border='0' />");echo("</td>");
      echo("<td>");echo($mvcfile->FileName);echo("</td>");
      echo("<td>");echo($mvcfile->FileSize);echo("</td>");
      echo("</tr>");
      
      //Moves the uploaded file to a new location.
      //$mvcfile->MoveTo("/uploads");
      //Copys the uploaded file to a new location.
      //$mvcfile->CopyTo("/uploads");
      //Deletes this instance.
      //$mvcfile->Delete();
     }
     echo("</table>");
    }

    ?>

        <input type='hidden' name='processedlist' value='<?php echo($processedlist) ?>' />

        <br /><br />
        <input type='submit' value="Submit Form" />
        Now: <?php echo(date("H:i:s",time())) ?>
        
       </form>
       
        
     </div>
    </body>
    </html>

View as RSS news feed in XML