I have a member based website and what I have built a system where registered users can submit reviews. Those are submitted to a MySQL database using PHP and then queried out onto a review page, like review.php?id=246. However, I want users to be able to upload an image, store the image in a folder on the server, store the filename in the MySQL database then query it all out onto 开发者_如何学JAVAmy review.php page for each individual one. How would I do this?
Thanks in advance guys!
Let's say you have table with columns id, review_id, img
. Also id column must be autoincremented int. Create following folders: photos
which includes folders named original and resized
.
Code below will resize image, save original and resized image, and set it's address into db table. I hope it will work for you
<?php
/* DB connection*/
require 'db.php';
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/*registration sheck*/
$submit=$db->real_escape_string( $_POST['submit']);
$review=$db->real_escape_string($_POST['review']);
function createThumb($upfile, $dstfile, $max_width, $max_height){
$size = getimagesize($upfile);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height)) {
$tn_width = $width;
$tn_height = $height;
} elseif (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
if($size['mime'] == "image/jpeg"){
$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imageinterlace( $dst, true);
ImageJpeg($dst, $dstfile, 100);
} else if ($size['mime'] == "image/png"){
$src = ImageCreateFrompng($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
Imagepng($dst, $dstfile);
} else {
$src = ImageCreateFromGif($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imagegif($dst, $dstfile);
}
}
if($submit){
$result = $db->query("INSERT INTO reviews (`review_id`) VALUE '$review'") or die($db->error));
$review_id = $db->insert_id;
if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') {
$ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1);
$imgNormal = 'img'.$new_user_id.$ext;
$normalDestination = "photos/original/" . $imgNormal;
$httprootmedium = "photos/resized/" . $imgNormal;
move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination);
createThumb($normalDestination,$httprootmedium,200,300); #For 500x300 Image
}
$result = $db->query("UPDATE review SET img='$imgNormal' WHERE id='$review_id'") or
?>
精彩评论