Passing Filename Variable To mySql Database

  •  02-04-2012, 12:55 PM

    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:
View Complete Thread