Image upload from, Form into Folder and MySQL

Thread Solved

Join Date: Aug 2008
Posts: 42
Reputation: AON07 is an unknown quantity at this point 
Solved Threads: 0
AON07's Avatar
AON07 AON07 is offline Offline
Light Poster

Image upload from, Form into Folder and MySQL

 
0
  #1
Aug 23rd, 2008
Hey all i'm fairly new to php and need some help with this code.
I'm not recieving any errors but it wont work.

what i'm trying to do is up load a picture from a form. i want to copy the picture into
a folder named "images" then keep all the other form information and the image path in a MySQL db. so later on i can display the images from a certain category on a web page.

DB table looks like this:

Table name: photos

id, image_name, caption, image_path, category,


Form Code:

  1. <form enctype="multipart/form-data" name="addimage" action="imageupload.php" method="post">
  2. <tr>
  3.  
  4. <td>
  5. Select Category:
  6. <br />
  7. <?php
  8. include 'Images_db_connect.php';
  9.  
  10. // Connect to server and select database.
  11. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  12. mysql_select_db("$db_name")or die("cannot select DB");
  13.  
  14. $sql = mysql_query("SELECT id,category_name FROM categories ORDER BY category_name");
  15. $row = mysql_fetch_array($sql);
  16. ?>
  17.  
  18. <select name="Category_name">
  19. <?php do{ ?>
  20. <option value="<?php echo $row['id']; ?>"><?php echo $row['category_name']; ?> </option>
  21. <?php } while($row = mysql_fetch_array($sql));?>
  22. </select>
  23.  
  24. <br />
  25. <br />
  26. Image Name:
  27. <br />
  28. <input type="text" name="image name" size="30">
  29. <br />
  30. <br />
  31. Image Caption:
  32. <br />
  33. <textarea name="Caption" rows="3" cols="30"></textarea>
  34. <br />
  35. <br />
  36. Select File:
  37. <br />
  38. <input type="file" name="Image" size="30">
  39. <br />
  40. <br />
  41. <input type="submit" name="upload" value="Upload Image">
  42. </td>
  43. </tr>
  44. </form>


upload code:

  1. <?php
  2. include 'Images_db_connect.php';
  3. $imagename = $_POST['image name'];
  4. $caption = $_POST['Caption'];
  5. $category = $_POST['Category_name'];
  6.  
  7. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  8. mysql_select_db("$db_name")or die("cannot select DB");
  9. //define a maxim size for the uploaded images in Kb
  10. define ("MAX_SIZE","75");
  11.  
  12. //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
  13. function getExtension($str) {
  14. $i = strrpos($str,".");
  15. if (!$i) { return ""; }
  16. $l = strlen($str) - $i;
  17. $ext = substr($str,$i+1,$l);
  18. return $ext;
  19. }
  20.  
  21. //This variable is used as a flag. The value is initialized with 0 (meaning no error found)
  22. //and it will be changed to 1 if an errro occures.
  23. //If the error occures the file will not be uploaded.
  24. $errors=0;
  25. //checks if the form has been submitted
  26. if(isset($_POST['Submit']))
  27. {
  28. //reads the name of the file the user submitted for uploading
  29. $image=$_FILES['image']['name'];
  30. //if it is not empty
  31. if ($image)
  32. {
  33. //get the original name of the file from the clients machine
  34. $filename = stripslashes($_FILES['image']['name']);
  35. //get the extension of the file in a lower case format
  36. $extension = getExtension($filename);
  37. $extension = strtolower($extension);
  38. //if it is not a known extension, we will suppose it is an error and will not upload the file,
  39. //otherwise we will do more tests
  40. if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
  41. {
  42. //print error message
  43. echo '<h1>Unknown extension!</h1>';
  44. $errors=1;
  45. }
  46. else
  47. {
  48. //get the size of the image in bytes
  49. //$_FILES['image']['tmp_name'] is the temporary filename of the file
  50. //in which the uploaded file was stored on the server
  51. $size=filesize($_FILES['image']['tmp_name']);
  52.  
  53. //compare the size with the maxim size we defined and print error if bigger
  54. if ($size > MAX_SIZE*1024)
  55. {
  56. echo '<h1>You have exceeded the size limit!</h1>';
  57. $errors=1;
  58. }
  59.  
  60. //we will give an unique name, for example the time in unix time format
  61. $image_name=time().'.'.$extension;
  62. //the new name will be containing the full path where will be stored (images folder)
  63. $newname="images/".$image_name;
  64. //we verify if the image has been uploaded, and print error instead
  65. $copied = copy($_FILES['image']['tmp_name'], $newname);
  66. if (!$copied)
  67. {
  68. $sql = "INSERT into photos (image_name, caption, image_path, category) VALUES ('$imagename', '$caption', '$newname', '$category')";
  69. mysql_query($sql) or die(mysql_error());
  70. echo '<h1>Copy unsuccessfull!</h1>';
  71. $errors=1;
  72. }}}}
  73.  
  74. //If no errors registred, print the success message
  75. if(isset($_POST['Submit']) && !$errors)
  76. {
  77. echo "<h1>File Uploaded Successfully!</h1>";
  78. }
  79. mysql_close();
  80.  
  81. ?>
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 109
Reputation: architact is an unknown quantity at this point 
Solved Threads: 7
architact's Avatar
architact architact is offline Offline
Junior Poster

Re: Image upload from, Form into Folder and MySQL

 
0
  #2
Aug 23rd, 2008
Instead of exploding and getting the extension use the mime type of the images (images/gif for firefox and safari, images/pgif for IExplorer). Because if you use the method of getting extension then the users will upload any thing after changing the files extension. e.g if a file is doc.docx they will rename it as doc.gif then your code will accept it.

  1. <?php
  2. $typ = $_FILES['file']['type'];
  3. if(isset($FILES['file']['name'])){
  4. if($typ == "images/gif" || $typ == "images/png" || $typ == "images/jpeg" || $typ == "images/pgif ||$typ" == "images/ppng" || $typ == "images/pjpeg"){
  5. $uploddir = "images/";
  6. $uploadimages = $uploaddir.$_FILES['file']['name'];
  7. if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadimages)){
  8. //insert data or other collected info into db.
  9. }
  10. }
  11. }
  12. ?>
Using this code you don't have to worry about getting extension or lowering extension. Just use a check of file size. This code will allow user to upload .jpg, .gif, .png files.
Last edited by architact; Aug 24th, 2008 at 12:26 am.
If you think we fight for money and you fight for honor, then remember everyone fights for the thing they don't have...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 42
Reputation: AON07 is an unknown quantity at this point 
Solved Threads: 0
AON07's Avatar
AON07 AON07 is offline Offline
Light Poster

Re: Image upload from, Form into Folder and MySQL

 
0
  #3
Aug 25th, 2008
K i need some major help on this. I am really new to php and have a feeling i'm along way from getting this correct. Is there someone willing to explain in great detail of what everything does and the process of uplaoding an image to a folder and then keeping the path in a database?

Here is the code i have so far, and i get the following error.

"Parse error: syntax error, unexpected T_IS_EQUAL in C:\xampp\htdocs\imageupload.php on line 12"

here is the code i have so far:

  1. <?php
  2. include 'Images_db_connect.php';
  3. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  4. mysql_select_db("$db_name")or die("cannot select DB");
  5. //define a maxim size for the uploaded images in Kb
  6. define ("MAX_SIZE","75");
  7.  
  8. if(isset($_POST['Submit']))
  9.  
  10. $typ = $_FILES['file']['type'];
  11. if(isset($FILES['file']['name'])){
  12. if($typ == "images/gif" || $typ == "images/png" || $typ == "images/jpeg" || $typ == "images/pgif ||$typ" == "images/ppng" || $typ == "images/pjpeg"){
  13. $uploddir = "images/";
  14. $uploadimages = $uploaddir.$_FILES['file']['name'];
  15. if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadimages)){
  16.  
  17. $imagename = 'image name';
  18. $caption = 'caption';
  19. $category = 'category_name';
  20.  
  21. $sql = "INSERT into photos (image_name, caption, image_path, category) VALUES ('$imagename', '$caption', '$uploadimages', '$category')";
  22. mysql_query($sql) or die(mysql_error());
  23. echo '<h1>Copy unsuccessfull!</h1>';
  24. $errors=1;
  25. }}}}
  26.  
  27. //If no errors registred, print the success message
  28. if(isset($_POST['Submit']) && !$errors)
  29. {
  30. echo "<h1>File Uploaded Successfully!</h1>";
  31. }
  32. mysql_close();
  33.  
  34. ?>
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 109
Reputation: architact is an unknown quantity at this point 
Solved Threads: 7
architact's Avatar
architact architact is offline Offline
Junior Poster

Re: Image upload from, Form into Folder and MySQL

 
0
  #4
Aug 25th, 2008
  1. if(isset($_POST['Submit']))

Where is starting bracket of if body??
If you think we fight for money and you fight for honor, then remember everyone fights for the thing they don't have...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 42
Reputation: AON07 is an unknown quantity at this point 
Solved Threads: 0
AON07's Avatar
AON07 AON07 is offline Offline
Light Poster

Re: Image upload from, Form into Folder and MySQL

 
0
  #5
Aug 26th, 2008
To be completely honest i'm not 100% sure of the placment of these tags.
if ya cant tell i'm a total newbie. I really want to learn this so if you can please give me details of why things are done and where they are placed. I really appreciate your help.

I hope this is what you meant.

  1. if(isset($_POST['Submit'])){
  2. {
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 42
Reputation: AON07 is an unknown quantity at this point 
Solved Threads: 0
AON07's Avatar
AON07 AON07 is offline Offline
Light Poster

Re: Image upload from, Form into Folder and MySQL

 
0
  #6
Aug 26th, 2008
Also i am still getting an error on line 10. any ideas anyone?

  1. #10 $typ = $_FILES['file']['type'];
  2. #11 if(isset($FILES['file']['name'])){
  3. #12 if($typ == "images/gif" || $typ == "images/png" || $typ == "images/jpeg" || $typ == "images/pgif ||$typ" == "images/ppng" || $typ == "images/pjpeg"){
  4. #13 $uploddir = "images/";
  5. #14 $uploadimages = $uploaddir.$_FILES['file']['name'];
  6. #15 if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadimages)){
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 109
Reputation: architact is an unknown quantity at this point 
Solved Threads: 7
architact's Avatar
architact architact is offline Offline
Junior Poster

Re: Image upload from, Form into Folder and MySQL

 
0
  #7
Aug 26th, 2008
on line 11 $FILES is wrong $_FILES is correct, I will give you details but right now i am in a hurry. See ya later.
If you think we fight for money and you fight for honor, then remember everyone fights for the thing they don't have...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 42
Reputation: AON07 is an unknown quantity at this point 
Solved Threads: 0
AON07's Avatar
AON07 AON07 is offline Offline
Light Poster

Re: Image upload from, Form into Folder and MySQL

 
0
  #8
Aug 26th, 2008
Well that fixed that line 10 error now its a line 12 error..
thanks for your help, just let me know when ya get a chance to look at it.

Thanks again
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 109
Reputation: architact is an unknown quantity at this point 
Solved Threads: 7
architact's Avatar
architact architact is offline Offline
Junior Poster

Re: Image upload from, Form into Folder and MySQL

 
0
  #9
Aug 26th, 2008
please send me the complete code.
If you think we fight for money and you fight for honor, then remember everyone fights for the thing they don't have...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 42
Reputation: AON07 is an unknown quantity at this point 
Solved Threads: 0
AON07's Avatar
AON07 AON07 is offline Offline
Light Poster

Re: Image upload from, Form into Folder and MySQL

 
0
  #10
Aug 26th, 2008
Here is the code. The form code is in the first post.

  1. <?php
  2. include 'Images_db_connect.php';
  3. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  4. mysql_select_db("$db_name")or die("cannot select DB");
  5. //define a maxim size for the uploaded images in Kb
  6. define ("MAX_SIZE","75");
  7.  
  8. if(isset($_POST['Submit']))
  9.  
  10. $typ = $_FILES['file']['type'];
  11. if(isset($FILES['file']['name'])){
  12. if($typ == "images/gif" || $typ == "images/png" || $typ == "images/jpeg" || $typ == "images/pgif ||$typ" == "images/ppng" || $typ == "images/pjpeg"){
  13. $uploddir = "images/";
  14. $uploadimages = $uploaddir.$_FILES['file']['name'];
  15. if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadimages)){
  16.  
  17. $imagename = 'image name';
  18. $caption = 'caption';
  19. $category = 'category_name';
  20.  
  21. $sql = "INSERT into photos (image_name, caption, image_path, category) VALUES ('$imagename', '$caption', '$uploadimages', '$category')";
  22. mysql_query($sql) or die(mysql_error());
  23. echo '<h1>Copy unsuccessfull!</h1>';
  24. $errors=1;
  25. }}}}
  26.  
  27. //If no errors registred, print the success message
  28. if(isset($_POST['Submit']) && !$errors)
  29. {
  30. echo "<h1>File Uploaded Successfully!</h1>";
  31. }
  32. mysql_close();
  33.  
  34. ?><?php
  35. include 'Images_db_connect.php';
  36. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  37. mysql_select_db("$db_name")or die("cannot select DB");
  38. //define a maxim size for the uploaded images in Kb
  39. define ("MAX_SIZE","75");
  40.  
  41. if(isset($_POST['Submit'])){
  42. }
  43. $typ = $_FILES['file']['type'];
  44. if(isset($FILES['file']['name'])){
  45. if($typ == "images/gif" || $typ == "images/png" || $typ == "images/jpeg" || $typ == "images/pgif ||$typ" == "images/ppng" || $typ == "images/pjpeg"){
  46. $uploddir = "images/";
  47. $uploadimages = $uploaddir.$_FILES['file']['name'];
  48. if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadimages)){
  49.  
  50. $imagename = 'image name';
  51. $caption = 'caption';
  52. $category = 'category_name';
  53.  
  54. $sql = "INSERT into photos (image_name, caption, image_path, category) VALUES ('$imagename', '$caption', '$uploadimages', '$category')";
  55. mysql_query($sql) or die(mysql_error());
  56. echo '<h1>Copy unsuccessfull!</h1>';
  57. $errors=1;
  58. }}}}
  59.  
  60. //If no errors registred, print the success message
  61. if(isset($_POST['Submit']) && !$errors)
  62. {
  63. echo "<h1>File Uploaded Successfully!</h1>";
  64. }
  65. mysql_close();
  66.  
  67. ?>
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3435 | Replies: 32
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC