irwan

Uploading Images with PHP

Mar 10th, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.03 KB | None | 0 0
  1. DATABASE
  2.  
  3. CREATE TABLE `people` (
  4. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  5. `fname` VARCHAR( 30 ) NOT NULL ,
  6. `lname` VARCHAR( 40 ) NOT NULL ,
  7. `filename` VARCHAR( 50 ) NOT NULL
  8. ) ENGINE = MYISAM ;
  9.  
  10.  
  11.  
  12. conn.php
  13.  
  14. <?php
  15.  
  16. // Input your information for the database here
  17.  
  18. // Host name
  19. $host = "localhost";
  20.  
  21. // Database username
  22. $username = "username";
  23.  
  24. // Database password
  25. $password = "password";
  26.  
  27. // Name of database
  28. $database = "db_name";
  29.  
  30. $conn = mysql_connect($host, $username, $password) or die ("Could not connect");
  31. $db = mysql_select_db($database, $conn) or die ("Could not select DB");
  32. ?>
  33.  
  34.  
  35.  
  36.  
  37.  
  38. index.php
  39.  
  40. <?php
  41. // Start a session for displaying any form errors
  42. session_start();
  43. ?>
  44. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  45.       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  46.  
  47. <html xmlns="http://www.w3.org/1999/xhtml">
  48.     <head>
  49.         <title>Dream in code tutorial</title>
  50.  
  51.         <style type="text/css">
  52.             label
  53.             {
  54.                 float: left;
  55.                 text-align: right;
  56.                 margin-right: 10px;
  57.                 width: 100px;
  58.                 color: black;
  59.             }
  60.  
  61.             #submit
  62.             {
  63.                 float: left;
  64.                 margin-top: 5px;
  65.                 position: relative;
  66.                 left: 110px;
  67.             }
  68.  
  69.             #error
  70.             {
  71.                 color: red;
  72.                 font-weight: bold;
  73.                 font-size: 16pt;
  74.             }
  75.         </style>
  76.     </head>
  77.  
  78.     <body>
  79.    
  80.         <div>
  81.                 <?php
  82.                 if (isset($_SESSION['error']))
  83.                 {
  84.                     echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";
  85.                     unset($_SESSION['error']);
  86.                 }
  87.                 ?>
  88.                 <form action="upload.php" method="post" enctype="multipart/form-data">
  89.                 <p>
  90.                     <label>First Name</label>
  91.                     <input type="text" name="fname" /><br />
  92.  
  93.                     <label>Last Name</label>
  94.                     <input type="text" name="lname" /><br />
  95.  
  96.                     <label>Upload Image</label>
  97.                     <input type="file" name="image" /><br />
  98.                     <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  99.                     <input type="submit" id="submit" value="Upload" />
  100.                 </p>
  101.                 </form>
  102.         </div>
  103.     </body>
  104. </html>
  105.  
  106.  
  107.  
  108.  
  109. upload.php
  110.  
  111. <?php
  112. // Start a session for error reporting
  113. session_start();
  114.  
  115. // Call our connection file
  116. require("conn.php");
  117.  
  118. // Check to see if the type of file uploaded is a valid image type
  119. function is_valid_type($file)
  120. {
  121.     // This is an array that holds all the valid image MIME types
  122.     $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");
  123.  
  124.     if (in_array($file['type'], $valid_types))
  125.         return 1;
  126.     return 0;
  127. }
  128.  
  129. // Just a short function that prints out the contents of an array in a manner that's easy to read
  130. // I used this function during debugging but it serves no purpose at run time for this example
  131. function showContents($array)
  132. {
  133.     echo "<pre>";
  134.     print_r($array);
  135.     echo "</pre>";
  136. }
  137.  
  138. // Set some constants
  139.  
  140. // This variable is the path to the image folder where all the images are going to be stored
  141. // Note that there is a trailing forward slash
  142. $TARGET_PATH = "images/";
  143.  
  144. // Get our POSTed variables
  145. $fname = $_POST['fname'];
  146. $lname = $_POST['lname'];
  147. $image = $_FILES['image'];
  148.  
  149. // Sanitize our inputs
  150. $fname = mysql_real_escape_string($fname);
  151. $lname = mysql_real_escape_string($lname);
  152. $image['name'] = mysql_real_escape_string($image['name']);
  153.  
  154. // Build our target path full string.  This is where the file will be moved do
  155. // i.e.  images/picture.jpg
  156. $TARGET_PATH .= $image['name'];
  157.  
  158. // Make sure all the fields from the form have inputs
  159. if ( $fname == "" || $lname == "" || $image['name'] == "" )
  160. {
  161.     $_SESSION['error'] = "All fields are required";
  162.     header("Location: index.php");
  163.     exit;
  164. }
  165.  
  166. // Check to make sure that our file is actually an image
  167. // You check the file type instead of the extension because the extension can easily be faked
  168. if (!is_valid_type($image))
  169. {
  170.     $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
  171.     header("Location: index.php");
  172.     exit;
  173. }
  174.  
  175. // Here we check to see if a file with that name already exists
  176. // You could get past filename problems by appending a timestamp to the filename and then continuing
  177. if (file_exists($TARGET_PATH))
  178. {
  179.     $_SESSION['error'] = "A file with that name already exists";
  180.     header("Location: index.php");
  181.     exit;
  182. }
  183.  
  184. // Lets attempt to move the file from its temporary directory to its new home
  185. if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
  186. {
  187.     // NOTE: This is where a lot of people make mistakes.
  188.     // We are *not* putting the image into the database; we are putting a reference to the file's location on the server
  189.     $sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')";
  190.     $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
  191.     header("Location: images.php");
  192.     exit;
  193. }
  194. else
  195. {
  196.     // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
  197.     // Make sure you chmod the directory to be writeable
  198.     $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
  199.     header("Location: index.php");
  200.     exit;
  201. }
  202. ?>
  203.  
  204. OKAY ! REMEMBER! create  images directory
Advertisement
Add Comment
Please, Sign In to add comment