By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,631 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,631 IT Pros & Developers. It's quick & easy.

pls post mutiple image upload code & save filename in mysql

P: 2
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading.

i develop program,which can upload many file in folder.problem is,am unable to save my file name in to database.because i used same name for all input file type as file[]. i dont know get name of file.below i presented my coding for ur view.fed up with my coding..pls guide with new coding r suggest in my coding

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // make a note of the current working directory relative to root.
  4. $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
  5.  
  6. // make a note of the location of the upload handler
  7. $uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.php';
  8.  
  9. // set a max file size for the html upload form
  10. $max_file_size = 30000000000; // size in bytes
  11.  
  12. // now echo the html page
  13. ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  14. "http://www.w3.org/TR/html4/strict.dtd">
  15.  
  16. <html lang="en">
  17. <head>
  18. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  19.  
  20. <link rel="stylesheet" type="text/css" href="stylesheet.css">
  21.  
  22. <title>Upload form</title>
  23.  
  24. </head>
  25.  
  26. <body>
  27.  
  28. <form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">
  29.  
  30. <h1>
  31. Upload form
  32. </h1>
  33.  
  34. <p>
  35. <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
  36. </p>
  37.  
  38. <p>
  39. <label for="file1">File to upload:</label>
  40. <input id="file1" type="file" name="file[]">
  41. </p>
  42.  
  43. <p>
  44. <label for="file2">File to upload:</label>
  45. <input id="file2" type="file" name="file[]">
  46. </p>
  47.  
  48. <p>
  49. <label for="file3">File to upload:</label>
  50. <input id="file3" type="file" name="file[]">
  51. </p>
  52.  
  53. <p>
  54. <label for="submit">Press to...</label>
  55. <input id="submit" type="submit" name="submit" value="Upload us!">
  56. </p>
  57.  
  58. </form>
  59.  
  60.  
  61. </body>
  62.  
  63. </html>
  64.  
  65. <?php 
  66.  
  67. // make a note of the current working directory, relative to root.
  68. $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
  69.  
  70. // make a note of the directory that will recieve the uploaded files
  71. $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';
  72.  
  73. // make a note of the location of the upload form in case we need it
  74. $uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.php';
  75.  
  76. // make a note of the location of the success page
  77. $uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';
  78.  
  79. // name of the fieldname used for the file in the HTML form
  80. $fieldname = 'file';
  81.  
  82. //echo'<pre>';print_r($_FILES);exit;
  83.  
  84.  
  85.  
  86. // Now let's deal with the uploaded files
  87.  
  88. // possible PHP upload errors
  89. $errors = array(1 => 'php.ini max file size exceeded', 
  90. 2 => 'html form max file size exceeded', 
  91. 3 => 'file upload was only partial', 
  92. 4 => 'no file was attached');
  93.  
  94. // check the upload form was actually submitted else print form
  95. if(isset($_POST['submit']))
  96. {
  97. // or error('the upload form is neaded', $uploadForm);
  98.  
  99. // check if any files were uploaded and if 
  100. // so store the active $_FILES array keys
  101. $active_keys = array();
  102. foreach($_FILES[$fieldname]['name'] as $key => $filename)
  103. {
  104. if(!empty($filename))
  105. {
  106. $active_keys[] = $key;
  107. }
  108. }
  109.  
  110. // check at least one file was uploaded
  111. count($active_keys)
  112. or error('No files were uploaded', $uploadForm);
  113.  
  114. // check for standard uploading errors
  115. foreach($active_keys as $key)
  116. {
  117. ($_FILES[$fieldname]['error'][$key] == 0)
  118. or error($_FILES[$fieldname]['tmp_name'][$key].': '.$errors[$_FILES[$fieldname]['error'][$key]], $uploadForm);
  119. }
  120.  
  121. // check that the file we are working on really was an HTTP upload
  122. foreach($active_keys as $key)
  123. {
  124. @is_uploaded_file($_FILES[$fieldname]['tmp_name'][$key])
  125. or error($_FILES[$fieldname]['tmp_name'][$key].' not an HTTP upload', $uploadForm);
  126. }
  127.  
  128. // validation... since this is an image upload script we 
  129. // should run a check to make sure the upload is an image
  130. foreach($active_keys as $key)
  131. {
  132. @getimagesize($_FILES[$fieldname]['tmp_name'][$key])
  133. or error($_FILES[$fieldname]['tmp_name'][$key].' not an image', $uploadForm);
  134. }
  135.  
  136. // make a unique filename for the uploaded file and check it is 
  137. // not taken... if it is keep trying until we find a vacant one
  138. foreach($active_keys as $key)
  139. {
  140. $now = time();
  141. while(file_exists($uploadFilename[$key] = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'][$key]))
  142. {
  143. $now++;
  144. }
  145. }
  146.  
  147. // now let's move the file to its final and allocate it with the new filename
  148. foreach($active_keys as $key)
  149. {
  150. @move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key])
  151. or error('receiving directory insuffiecient permission', $uploadForm);
  152. }
  153.  
  154. // If you got this far, everything has worked and the file has been successfully saved.
  155. // We are now going to redirect the client to the success page.
  156. header('Location: ' . $uploadSuccess);
  157.  
  158. // make an error handler which will be used if the upload fails
  159. function error($error, $location, $seconds = 5)
  160. {
  161. header("Refresh: $seconds; URL=\"$location\"");
  162. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
  163. '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
  164. '<html lang="en">'."\n".
  165. ' <head>'."\n".
  166. ' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
  167. ' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
  168. ' <title>Upload error</title>'."\n\n".
  169. ' </head>'."\n\n".
  170. ' <body>'."\n\n".
  171. ' <div id="Upload">'."\n\n".
  172. ' <h1>Upload failure</h1>'."\n\n".
  173. ' <p>An error has occured: '."\n\n".
  174. ' <span class="red">' . $error . '...</span>'."\n\n".
  175. ' The upload form is reloading</p>'."\n\n".
  176. ' </div>'."\n\n".
  177. '</html>';
  178. exit;
  179. } // end error handler
  180. }
Sep 21 '09 #1
Share this Question
Share on Google+
1 Reply


Markus
Expert 5K+
P: 6,050
chennaibala,

Please understand that Bytes.com is a website dedicated to helping people solve their tech/IT related issues. It is not a site where you can request code and have it sent to you - that is against our forum guidelines[1].

Furthermore, when you post code, please use [code] your code goes here... [/code]. Again, this is a forum guideline[1].

If you would like to refine your question to target specific problems, and also show us that you are willing to do the work yourself, then post the question again as a reply to this thread.

Thanks,
Mark.

[1] Forum Guidelines
Sep 21 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.