Jump to content

need help on insert uploaded image name into database


lukelee

Recommended Posts

   1. <?php  
   2.  
   3. $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
   4.  
   5. $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';
   6.  
   7. $uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.form.php';
   8.  
   9. $uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.success.php';
  10.  
  11. $fieldname = 'file';
  12.  
  13.  
  14. $errors = array(1 => 'php.ini max file size exceeded',
  15.                 2 => 'html form max file size exceeded',
  16.                 3 => 'file upload was only partial',
  17.                 4 => 'no file was attached');
  18.  
  19. isset($_POST['submit'])
  20.     or error('the upload form is neaded', $uploadForm);
  21.  
  22. $active_keys = array();
  23. foreach($_FILES[$fieldname]['name'] as $key => $filename)
  24. {
  25.     if(!empty($filename))
  26.     {
  27.         $active_keys[] = $key;
  28.     }
  29. }
  30.  
  31. count($active_keys)
  32.     or error('No files were uploaded', $uploadForm);
  33.        
  34. foreach($active_keys as $key)
  35. {
  36.     ($_FILES[$fieldname]['error'][$key] == 0)
  37.         or error($_FILES[$fieldname]['tmp_name'][$key].': '.$errors[$_FILES[$fieldname]['error'][$key]], $uploadForm);
  38. }
  39.    
  40. foreach($active_keys as $key)
  41. {
  42.     @is_uploaded_file($_FILES[$fieldname]['tmp_name'][$key])
  43.         or error($_FILES[$fieldname]['tmp_name'][$key].' not an HTTP upload', $uploadForm);
  44. }
  45.    
  46.  
  47. foreach($active_keys as $key)
  48. {
  49.     @getimagesize($_FILES[$fieldname]['tmp_name'][$key])
  50.         or error($_FILES[$fieldname]['tmp_name'][$key].' not an image', $uploadForm);
  51. }
  52.    
  53.  
  54. foreach($active_keys as $key)
  55. {
  56.     $now = time();
  57.     while(file_exists($uploadFilename[$key] = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'][$key]))
  58.     {
  59.         $now++;
  60.     }
  61.  require_once('db.php');
  62. $query = mysql_query("UPDATE image SET imagedata = '$uploadFilename[$key]' WHERE pid = '1'");
  63.
  64.  
  65. }
  66.  
  67. foreach($active_keys as $key)
  68. {
  69.     @move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key])
  70.         or error('receiving directory insuffiecient permission', $uploadForm);
  71. }
  72.  
  73.  
  74. header('Location: ' .$uploadSuccess);
  75.  
  76. function error($error, $location, $seconds = 5)
  77. {
  78.     header("Refresh: $seconds; URL=\"$location\"");
  79.     echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
  80.     '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
  81.     '<html lang="en">'."\n".
  82.     '   <head>'."\n".
  83.     '       <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
  84.     '       <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
  85.     '   <title>Upload error</title>'."\n\n".
  86.     '   </head>'."\n\n".
  87.     '   <body>'."\n\n".
  88.     '   <div id="Upload">'."\n\n".
  89.     '       <h1>Upload failure</h1>'."\n\n".
  90.     '       <p>An error has occured: '."\n\n".
  91.     '       <span class="red">' . $error . '...</span>'."\n\n".
  92.     '       The upload form is reloading</p>'."\n\n".
  93.     '    </div>'."\n\n".
  94.     '</html>';
  95.     exit;
  96. }
  97.  
  98. ?>

 

this is my code, what i am doing is people can upload image from back-end, then the image in fron-end will change, its a very simple cms function. what i have tryed is, save the image url in database, table name is image, once people upload a image, the imagedata change to the new image name($query = mysql_query("UPDATE image SET imagedata = '$uploadFilename[$key]' WHERE pid = '1'");), then the image on the frontend will be changed to the new image, but what i have got in imagedata is something like: /var/virtual/web/w0333/subdomain/luke//test3/uploaded_files/book2.jpg, i just want a single image name, book2.jpg anyone can help me on this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.