Problem with uploading images to website

Thread Solved

Join Date: May 2008
Posts: 67
Reputation: Kavitha Butchi is an unknown quantity at this point 
Solved Threads: 3
Kavitha Butchi Kavitha Butchi is offline Offline
Junior Poster in Training

Problem with uploading images to website

 
0
  #1
Jun 4th, 2008
Hello all,
I am trying to code for user's profile pic uploading and resizing the image.
I am using the following code and I dont know where I am going wrong!

  1. <?php
  2.  
  3. //This is the directory where images will be saved
  4. $target1 = "images/";
  5. $target = $target1 . basename( $_FILES['photo']['name']);
  6.  
  7. //This gets all the other information from the form
  8. $name=$_POST['name'];
  9. $pic=($_FILES['photo']['name']);
  10.  
  11. // Connects to your Database
  12. database connections
  13.  
  14. //Writes the information to the database
  15. mysql_query("INSERT INTO `example` VALUES ('$name', '$pic')") ;
  16.  
  17. //Writes the photo to the server
  18. if(move_uploaded_file($_FILES['photo']['tmp'], $target))
  19. {
  20.  
  21. //Tells you if its all ok
  22. echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
  23. }
  24. else {
  25. echo $_FILES['photo']['tmp'];
  26. echo "Sorry, there was a problem uploading your file.";
  27. }
  28.  
  29. $data = mysql_query("SELECT * FROM example")
  30. or die(mysql_error());
  31.  
  32. while($info = mysql_fetch_array( $data ))
  33. {
  34.  
  35. //Outputs the image and other data
  36. echo "<b>Name:</b> ".$info['name'] . "<br> ";
  37.  
  38. echo "<img src=http://www.websitename.com/images/".$info['photo'] ."> <br>";
  39.  
  40.  
  41. }
  42. ?>

Whats happening here is name and image name are getting stored in the database but nothing is stored in the images folder in the server. I am not getting the image displayed .
Can anyone suggest whats the exact method if the below code is wrong somewhere. Also any suggestions about how to resize the image uploaded.

Thank you in advance.
Last edited by Kavitha Butchi; Jun 4th, 2008 at 3:57 pm.
Kavitha
I Love My Indonesia.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 24
Reputation: chitra1 is an unknown quantity at this point 
Solved Threads: 1
chitra1 chitra1 is offline Offline
Newbie Poster

Re: Problem with uploading images to website

 
0
  #2
Jun 4th, 2008
hello Kavitha,

This is my code which runs well. In fact I got it from this forum itself.

  1.  
  2.  
  3.  
  4.  
  5. <?php
  6. include("db_connect.php");
  7.  
  8. //Change this to the name of the page
  9. $thispage = 'up.php';
  10.  
  11. //Set the allowed file types
  12. $types = array("gif","jpeg","jpg","png");
  13.  
  14. //Set max size of image (in MB)
  15. $maxSize = '40';
  16.  
  17. //Set upload directory
  18. $uploadDir = 'upload';
  19.  
  20. //This function get the extension of the image
  21. function getExtension($str) {
  22. $i = strrpos($str,".");
  23. if (!$i) {
  24. return "";
  25. }
  26. $l = strlen($str) - $i;
  27. $ext = substr($str,$i+1,$l);
  28. return $ext;
  29. }
  30.  
  31.  
  32. if (isset($_POST['submit'])) {
  33. if (empty($_FILES['image']['name'])) {
  34. $result = 'Please choose an image';
  35. }
  36. else {
  37. $imageName = $_FILES['image']['name'];
  38. $exten = getExtension($imageName);
  39. if (!in_array($exten,$types)) {
  40. $result = 'Unknown extension, please choose a different image';
  41. }
  42. else {
  43. $tmpFile = $_FILES['image']['tmp_name'];
  44. $size = filesize($tmpFile);
  45. if ($size > ($maxSize * 1024)) {
  46. $result = 'Image exceeds size limit, choose a smaller image';
  47. }
  48. else {
  49. $newName = $uploadDir . '/' . $imageName ;
  50. //. '.' . $exten;
  51. if (!copy($tmpFile,$newName)) {
  52. $result = 'Unable to upload image, try again';
  53. }
  54. else {
  55. $sql = "INSERT INTO `symbol` (symbol) VALUES ('<img src=" . $newName . " width=200>')";
  56. $query = mysql_query($sql) or die('Error: ' . mysql_error());
  57. $result = 'Image Uploaded Successfully';
  58. }
  59. }
  60. }
  61. }
  62. }
  63. $html =<<<HTML
  64. <form action="$thispage" method="post" enctype="multipart/form-data">
  65. <table border="0" cellspacing="0" cellpadding="5">
  66. <tr>
  67. <td align="right">Image:</td>
  68. <td align="left"><input type="file" name="image" /></td>
  69. </tr>
  70. <tr>
  71. <td align="right">Key:</td>
  72. <td align="left"><input type="text" name="key" /></td>
  73. </tr>
  74. <tr>
  75. <td colspan="2" align="center"><input type="submit" name="submit" value="Upload" /></td>
  76. </tr>
  77. <tr>
  78. <td colspan="2" align="center">$result</td>
  79. </tr>
  80. </table>
  81. </form>
  82. HTML;
  83.  
  84. echo $html;
  85.  
  86. ?>

The images are semt to the folder 'upload' an the filepath is sent to the database and as for retrieving and displaying the images:

  1.  
  2. <?php
  3.  
  4. include("db_connect.php");
  5.  
  6. error_reporting(E_ALL);
  7.  
  8. // get the image from the db
  9.  
  10. $sql = "SELECT symbol FROM symbol WHERE image_id=0";
  11.  
  12.  
  13.  
  14. // the result of the query
  15.  
  16. $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
  17.  
  18.  
  19.  
  20. // set the header for the image
  21.  
  22. // header("Content-type: image/jpeg");
  23. echo "<table >";
  24. echo "<tr><td align=center>";
  25. echo mysql_result($result, 0);
  26. echo "</td></tr></table>";
  27.  
  28.  
  29. // close the db link
  30.  
  31. mysql_close();

This code works but the problem is that I am not being able to resize the images. When displayed, it is being displayed on the whole page. If you get to know how to do this let me know.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Problem with uploading images to website

 
1
  #3
Jun 5th, 2008
thanks for posting my code so i didn't have to write it again.
Last edited by kkeith29; Jun 5th, 2008 at 1:42 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 24
Reputation: chitra1 is an unknown quantity at this point 
Solved Threads: 1
chitra1 chitra1 is offline Offline
Newbie Poster

Re: Problem with uploading images to website

 
0
  #4
Jun 5th, 2008
Hi,

Since it's ur code, can u please tell us how to resize the images retrieved from the database.
Last edited by chitra1; Jun 5th, 2008 at 3:21 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Problem with uploading images to website

 
0
  #5
Jun 5th, 2008
i resize them before i add them to do the database.

here is some code i have used on my last few projects (includes my upload code above):

  1. <?php
  2. include("db_connect.php");
  3.  
  4. //Change this to the name of the page
  5. $thispage = 'up.php';
  6.  
  7. //Set the allowed file types
  8. //NOTE ONLY JPEG and PNG file allowed because php doesn't have much image support for GIF
  9. $types = array("jpeg","jpg","png");
  10.  
  11. //Set max size of image (in MB)
  12. $maxSize = '40';
  13.  
  14. //Set maximum width and height of resized images (images will not be distorted)
  15. define('WIDTH',150);
  16. define('HEIGHT',150);
  17.  
  18. //Set temp directory where upload images will be stored before resizing
  19. $tempDir = 'temp';
  20.  
  21. //Set upload directory where resized images will be stored
  22. $uploadDir = 'upload';
  23.  
  24. //This function get the extension of the image
  25. function getExtension($str) {
  26. $i = strrpos($str,".");
  27. if (!$i) {
  28. return "";
  29. }
  30. $l = strlen($str) - $i;
  31. $ext = substr($str,$i+1,$l);
  32. return $ext;
  33. }
  34.  
  35. //This function resizes the image
  36. function make_thumb($img_name,$filename,$new_w,$new_h) {
  37. $ext = $this->getExtension($img_name);
  38. if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) {
  39. $src_img=imagecreatefromjpeg($img_name);
  40. }
  41. if(!strcmp("png",$ext)) {
  42. $src_img=imagecreatefrompng($img_name);
  43. }
  44. $old_x=imageSX($src_img);
  45. $old_y=imageSY($src_img);
  46. $ratio1=$old_x/$new_w;
  47. $ratio2=$old_y/$new_h;
  48. if($ratio1>$ratio2) {
  49. $thumb_w=$new_w;
  50. $thumb_h=$old_y/$ratio1;
  51. }
  52. else {
  53. $thumb_h=$new_h;
  54. $thumb_w=$old_x/$ratio2;
  55. }
  56. $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
  57. imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
  58. if(!strcmp("png",$ext)) {
  59. imagepng($dst_img,$filename);
  60. }
  61. else {
  62. imagejpeg($dst_img,$filename);
  63. }
  64. imagedestroy($dst_img);
  65. imagedestroy($src_img);
  66. }
  67.  
  68. if (isset($_POST['submit'])) {
  69. if (empty($_FILES['image']['name'])) {
  70. $result = 'Please choose an image';
  71. }
  72. else {
  73. $imageName = $_FILES['image']['name'];
  74. $exten = getExtension($imageName);
  75. if (!in_array($exten,$types)) {
  76. $result = 'Unknown extension, please choose a different image';
  77. }
  78. else {
  79. $tmpFile = $_FILES['image']['tmp_name'];
  80. $size = filesize($tmpFile);
  81. if ($size > ($maxSize * 1024)) {
  82. $result = 'Image exceeds size limit, choose a smaller image';
  83. }
  84. else {
  85. $time = time();
  86. $newName = $tempDir . '/' . $time . '.' . $exten;
  87. if (!copy($tmpFile,$newName)) {
  88. $result = 'Unable to upload image, try again';
  89. }
  90. else {
  91. $finalName = $uploadDir . '/' . $time . '.' . $exten;
  92. $make = make_thumb($newName,$finalName,WIDTH,HEIGHT);
  93. $sql = "INSERT INTO `symbol` (symbol) VALUES ('<img src=" . $finalName . " width=200>')";
  94. $query = mysql_query($sql) or die('Error: ' . mysql_error());
  95. $result = 'Image Uploaded Successfully';
  96. }
  97. }
  98. }
  99. }
  100. }
  101. $html =<<<HTML
  102. <form action="$thispage" method="post" enctype="multipart/form-data">
  103. <table border="0" cellspacing="0" cellpadding="5">
  104. <tr>
  105. <td align="right">Image:</td>
  106. <td align="left"><input type="file" name="image" /></td>
  107. </tr>
  108. <tr>
  109. <td align="right">Key:</td>
  110. <td align="left"><input type="text" name="key" /></td>
  111. </tr>
  112. <tr>
  113. <td colspan="2" align="center"><input type="submit" name="submit" value="Upload" /></td>
  114. </tr>
  115. <tr>
  116. <td colspan="2" align="center">$result</td>
  117. </tr>
  118. </table>
  119. </form>
  120. HTML;
  121.  
  122. echo $html;
  123.  
  124. ?>
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 67
Reputation: Kavitha Butchi is an unknown quantity at this point 
Solved Threads: 3
Kavitha Butchi Kavitha Butchi is offline Offline
Junior Poster in Training

Re: Problem with uploading images to website

 
0
  #6
Jun 6th, 2008
Thankyou for the code Chitra and KKeith but unfortunately it is not working to me. Can someone please help me out with my code.
Kavitha
I Love My Indonesia.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Problem with uploading images to website

 
0
  #7
Jun 6th, 2008
whats not working, are there any errors. did you change the variables in the script, what did you do to the script, did you make any other changes, ect.

by saying not working, it really doesn't help us find a solution.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 67
Reputation: Kavitha Butchi is an unknown quantity at this point 
Solved Threads: 3
Kavitha Butchi Kavitha Butchi is offline Offline
Junior Poster in Training

Re: Problem with uploading images to website

 
0
  #8
Jun 6th, 2008
Hello kkeith thankyou very much fr responding to my query.

I get nothing but empty page when I run this code.. seems like I need to change many variable names and am really confused where and which to change in the first case.

  1. $sql = "INSERT INTO `symbol` (symbol) VALUES ('<img src=" . $finalName . " width=200>')";

* => Does 'symbol' here needs to be replaced by my table name ?
and can you please tell me where else should I make changes other than giving database connections.

Thankyou in advance,
Kavitha
I Love My Indonesia.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Problem with uploading images to website

 
0
  #9
Jun 6th, 2008
you need to change the sql to suit your database. what does your table look like.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 67
Reputation: Kavitha Butchi is an unknown quantity at this point 
Solved Threads: 3
Kavitha Butchi Kavitha Butchi is offline Offline
Junior Poster in Training

Re: Problem with uploading images to website

 
0
  #10
Jun 6th, 2008
thank you fr responding.

my sql table has only two attributes,

name and pic

Can you please indicate where should I make specific changes!
Last edited by Kavitha Butchi; Jun 6th, 2008 at 11:03 pm.
Kavitha
I Love My Indonesia.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC