Uploadify JQuery & PHP/MySQL Database Control


/ Published in: jQuery
Save to your folder(s)

The uploaded file is attached to the "pid" of the recently inserted project.

the "pid" variable is retrieved from the database after the project has been entered


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. require "../../scr/config-data.php.inc";
  4. mysql_connect($host,$username,$password) or die
  5. ("Could Not Connect".mysql_error());
  6. mysql_select_db($db) or die ("Could Not Connect".mysql_error());
  7. $pid = $_POST['pid'];
  8.  
  9. if (!empty($_FILES)) {
  10. $selectAlias = mysql_query("SELECT * FROM project WHERE id = '$pid'");
  11. while($alias_result = mysql_fetch_array($selectAlias)){
  12. $alias = $alias_result['alias'];
  13. }
  14. $tempFile = $_FILES['Filedata']['tmp_name'];
  15. $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/' . $alias . '-';
  16. $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
  17. move_uploaded_file($tempFile,$targetFile);
  18. echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
  19.  
  20. $filePath = "proj/" .$alias . '-' . $_FILES['Filedata']['name'];
  21. mysql_query("INSERT INTO image VALUES (NULL, '$pid', '$filePath')") or die("Database Query Error: ". mysql_error());
  22. }
  23.  
  24. ?>
  25.  
  26. <html>
  27. <head>
  28. <link href="../mod/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
  29. <script type="text/javascript" src="../mod/uploadify/jquery-1.4.2.min.js"></script>
  30. <script type="text/javascript" src="../mod/uploadify/swfobject.js"></script>
  31. <script type="text/javascript" src="../mod/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
  32. <script type="text/javascript">
  33. var pid;
  34.  
  35. $(document).ready(function() {
  36. document.getElementById("files").style.display = "none";
  37. $('#file_upload').uploadify({
  38. 'uploader' : '../mod/uploadify/uploadify.swf',
  39. 'script' : '../mod/uploadify/uploadify.php',
  40. 'cancelImg' : '../mod/uploadify/cancel.png',
  41. 'folder' : '../proj',
  42. 'auto' : true,
  43. 'multi' : true,
  44. 'onError' : function (event,ID,fileObj,errorObj) {
  45. alert(errorObj.type + ' Error: ' + errorObj.info);
  46. },
  47. 'onSelectOnce': function(event,data) {
  48. $('#file_upload').uploadifySettings('scriptData', {'pid': pid});
  49. }
  50.  
  51. });
  52. });
  53. </script>
  54. <head>
  55. <body>
  56. <input id="file_upload" name="file_upload" type="file" />
  57. </body>
  58. <html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.