| | |
Problem with uploading images to website
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: May 2008
Posts: 67
Reputation:
Solved Threads: 3
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!
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.
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!
php Syntax (Toggle Plain Text)
<?php //This is the directory where images will be saved $target1 = "images/"; $target = $target1 . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['name']; $pic=($_FILES['photo']['name']); // Connects to your Database database connections //Writes the information to the database mysql_query("INSERT INTO `example` VALUES ('$name', '$pic')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { echo $_FILES['photo']['tmp']; echo "Sorry, there was a problem uploading your file."; } $data = mysql_query("SELECT * FROM example") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { //Outputs the image and other data echo "<b>Name:</b> ".$info['name'] . "<br> "; echo "<img src=http://www.websitename.com/images/".$info['photo'] ."> <br>"; } ?>
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.
I Love My Indonesia.
•
•
Join Date: Dec 2007
Posts: 24
Reputation:
Solved Threads: 1
hello Kavitha,
This is my code which runs well. In fact I got it from this forum itself.
The images are semt to the folder 'upload' an the filepath is sent to the database and as for retrieving and displaying the images:
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.
This is my code which runs well. In fact I got it from this forum itself.
PHP Syntax (Toggle Plain Text)
<?php include("db_connect.php"); //Change this to the name of the page $thispage = 'up.php'; //Set the allowed file types $types = array("gif","jpeg","jpg","png"); //Set max size of image (in MB) $maxSize = '40'; //Set upload directory $uploadDir = 'upload'; //This function get the extension of the image function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } if (isset($_POST['submit'])) { if (empty($_FILES['image']['name'])) { $result = 'Please choose an image'; } else { $imageName = $_FILES['image']['name']; $exten = getExtension($imageName); if (!in_array($exten,$types)) { $result = 'Unknown extension, please choose a different image'; } else { $tmpFile = $_FILES['image']['tmp_name']; $size = filesize($tmpFile); if ($size > ($maxSize * 1024)) { $result = 'Image exceeds size limit, choose a smaller image'; } else { $newName = $uploadDir . '/' . $imageName ; //. '.' . $exten; if (!copy($tmpFile,$newName)) { $result = 'Unable to upload image, try again'; } else { $sql = "INSERT INTO `symbol` (symbol) VALUES ('<img src=" . $newName . " width=200>')"; $query = mysql_query($sql) or die('Error: ' . mysql_error()); $result = 'Image Uploaded Successfully'; } } } } } $html =<<<HTML <form action="$thispage" method="post" enctype="multipart/form-data"> <table border="0" cellspacing="0" cellpadding="5"> <tr> <td align="right">Image:</td> <td align="left"><input type="file" name="image" /></td> </tr> <tr> <td align="right">Key:</td> <td align="left"><input type="text" name="key" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="Upload" /></td> </tr> <tr> <td colspan="2" align="center">$result</td> </tr> </table> </form> HTML; echo $html; ?>
The images are semt to the folder 'upload' an the filepath is sent to the database and as for retrieving and displaying the images:
PHP Syntax (Toggle Plain Text)
<?php include("db_connect.php"); error_reporting(E_ALL); // get the image from the db $sql = "SELECT symbol FROM symbol WHERE image_id=0"; // the result of the query $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); // set the header for the image // header("Content-type: image/jpeg"); echo "<table >"; echo "<tr><td align=center>"; echo mysql_result($result, 0); echo "</td></tr></table>"; // close the db link 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.
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):
here is some code i have used on my last few projects (includes my upload code above):
PHP Syntax (Toggle Plain Text)
<?php include("db_connect.php"); //Change this to the name of the page $thispage = 'up.php'; //Set the allowed file types //NOTE ONLY JPEG and PNG file allowed because php doesn't have much image support for GIF $types = array("jpeg","jpg","png"); //Set max size of image (in MB) $maxSize = '40'; //Set maximum width and height of resized images (images will not be distorted) define('WIDTH',150); define('HEIGHT',150); //Set temp directory where upload images will be stored before resizing $tempDir = 'temp'; //Set upload directory where resized images will be stored $uploadDir = 'upload'; //This function get the extension of the image function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This function resizes the image function make_thumb($img_name,$filename,$new_w,$new_h) { $ext = $this->getExtension($img_name); if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) { $src_img=imagecreatefromjpeg($img_name); } if(!strcmp("png",$ext)) { $src_img=imagecreatefrompng($img_name); } $old_x=imageSX($src_img); $old_y=imageSY($src_img); $ratio1=$old_x/$new_w; $ratio2=$old_y/$new_h; if($ratio1>$ratio2) { $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; } else { $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if(!strcmp("png",$ext)) { imagepng($dst_img,$filename); } else { imagejpeg($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); } if (isset($_POST['submit'])) { if (empty($_FILES['image']['name'])) { $result = 'Please choose an image'; } else { $imageName = $_FILES['image']['name']; $exten = getExtension($imageName); if (!in_array($exten,$types)) { $result = 'Unknown extension, please choose a different image'; } else { $tmpFile = $_FILES['image']['tmp_name']; $size = filesize($tmpFile); if ($size > ($maxSize * 1024)) { $result = 'Image exceeds size limit, choose a smaller image'; } else { $time = time(); $newName = $tempDir . '/' . $time . '.' . $exten; if (!copy($tmpFile,$newName)) { $result = 'Unable to upload image, try again'; } else { $finalName = $uploadDir . '/' . $time . '.' . $exten; $make = make_thumb($newName,$finalName,WIDTH,HEIGHT); $sql = "INSERT INTO `symbol` (symbol) VALUES ('<img src=" . $finalName . " width=200>')"; $query = mysql_query($sql) or die('Error: ' . mysql_error()); $result = 'Image Uploaded Successfully'; } } } } } $html =<<<HTML <form action="$thispage" method="post" enctype="multipart/form-data"> <table border="0" cellspacing="0" cellpadding="5"> <tr> <td align="right">Image:</td> <td align="left"><input type="file" name="image" /></td> </tr> <tr> <td align="right">Key:</td> <td align="left"><input type="text" name="key" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="Upload" /></td> </tr> <tr> <td colspan="2" align="center">$result</td> </tr> </table> </form> HTML; echo $html; ?>
•
•
Join Date: May 2008
Posts: 67
Reputation:
Solved Threads: 3
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.
* => 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,
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.
php Syntax (Toggle Plain Text)
$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.
I Love My Indonesia.
![]() |
Other Threads in the PHP Forum
- Previous Thread: PHP nested While loop
- Next Thread: Including external content
| Thread Tools | Search this Thread |
ajax apache api array basic beginner binary body broken cakephp checkbox class cms code cookies cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert interactive ip javascript job joomla js limit link login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop outofmemmory paging parse paypal pdf php procedure query radio ram random recursion regex remote return script search server sessions source space sql stored subdomain syntax system table tutorial unicode update upload url validation validator variable video web webapplications websitecontactform xml youtube






