PHP Flex File Upload


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



Copy this code and paste it in your HTML
  1. <?php
  2. /** *******************************************************************
  3.  * Working File Upload Server Script for Flex
  4.  *
  5.  * MySnippets
  6.  * Free for use
  7.  *
  8.  * @author Jonnie Spratley
  9.  * @contact [email protected]
  10.  ******************************************************************* */
  11. if (isset( $_POST['FileType']) && isset($_FILES['Filedata']) )
  12. {
  13.  
  14. /* Establish a connection to database */
  15. $link = mysql_connect('localhost', 'spratley_guest', 'guest') or die('Could not connect: ' . mysql_error());
  16.  
  17. /* Select the database */
  18. mysql_select_db( 'spratley_tutorials', $link ) or die ( mysql_error() );
  19.  
  20. /* Set the upload directory */
  21. $upload_dir = "uploads/";
  22.  
  23. /* PHP temp_name variable for file upload */
  24. $temp_name = $_FILES['Filedata']['tmp_name'];
  25.  
  26. /* PHP file_name variable sent from Flex */
  27. $file_name = $_FILES['Filedata']['name'];
  28.  
  29. /* PHP file_size variable sent from Flex */
  30. $file_size = $_FILES['Filedata']['size'];
  31.  
  32. /* PHP file_type variable sent from Flex */
  33. $file_type = $_FILES['Filedata']['type'];
  34.  
  35. /* PHP file_ext variable set from Flex */
  36. $file_ext = $_FILES['Filedata']['extension'];
  37.  
  38. /* Set the file_url to the hosturl and the updload directory and filename */
  39. $file_url = $_SERVER['HTTP_HOST'] . $upload_dir . $file_name;
  40.  
  41. /* Replace any computer garbage */
  42. $file_name = str_replace("\","",$file_name);
  43.  
  44. /* Replace any garbage */
  45. $file_name = str_replace("'","",$file_name);
  46.  
  47. /* Set up the filepath */
  48. $file_path = $upload_dir.$file_name;
  49.  
  50.  
  51. /* Insert info into fle_uploads table in your mysql database */
  52. $insert = "INSERT INTO flex_uploads ( file_name, file_size, file_type, file_ext, file_url )
  53. VALUES ( '$file_name',
  54. '$file_size',
  55. '$file_type',
  56. '$file_ext',
  57. '$file_url' )";
  58.  
  59. /* Execute the query */
  60. $query = mysql_query($insert) or die(mysql_error());
  61.  
  62. /* Get the last insert id */
  63. $lastid = mysql_insert_id();
  64.  
  65. /* Move the uploaded file */
  66. $result = move_uploaded_file( $temp_name, $file_path );
  67.  
  68. /* Set the content type for the browser */
  69. header("Content-type: text/xml");
  70.  
  71. /* Set the header xml version */
  72. $xml_output = "<?xml version="1.0"?>n";
  73.  
  74. /* Set the root node for the xml */
  75. $xml_output .= "<results>n";
  76.  
  77. /* If file was moved and inserted */
  78. if ( $result )
  79. {
  80. /* Build the XML if file was successful */
  81. /* Set the child node */
  82. $xml_output .= "t<result>n";
  83.  
  84. /* status */
  85. $xml_output .= "tt<status>" . "Successful" . "</status>n";
  86.  
  87. /* file_name variable */
  88. $xml_output .= "tt<name>" . $file_name . "</name>n";
  89.  
  90. /* file_size variable */
  91. $xml_output .= "tt<size>" . $file_size . "</size>n";
  92.  
  93. /* file_type variable */
  94. $xml_output .= "tt<type>" . $file_type . "</type>n";
  95.  
  96. /* file_url variable */
  97. $xml_output .= "tt<url>" . $file_url . "</url>n";
  98.  
  99. /* Close the child result now and print the child result node out */
  100. $xml_output .= "t</result>n";
  101.  
  102. }
  103.  
  104. } else {
  105.  
  106. /* Set the content type for the browser */
  107. header("Content-type: text/xml");
  108.  
  109. /* Set the header xml version */
  110. $xml_output = "<?xml version="1.0"?>n";
  111.  
  112. /* Set the root node for the xml */
  113. $xml_output .= "<results>n";
  114.  
  115. /* Build the XML if file was un-successful */
  116. /* Set the child node */
  117. $xml_output .= "t<result>n";
  118.  
  119. /* status */
  120. $xml_output .= "tt<status>" . "Error" . "</status>n";
  121.  
  122. /* file_name variable */
  123. $xml_output .= "tt<name>" . $file_name . "</name>n";
  124.  
  125. /* file_name variable */
  126. $xml_output .= "tt<message>" . "There was a problem uploading your file." . "</message>n";
  127.  
  128.  
  129. /* Close the child result now and print the child result node out */
  130. $xml_output .= "t</result>n";
  131.  
  132. }
  133.  
  134. /* Close the parent results node */
  135. $xml_output .= "</results>";
  136.  
  137. /* Output all of the xml */
  138. echo $xml_output;
  139.  
  140.  
  141.  
  142. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.