Hello, I'm having trouble uploading all the various data to the database, can you help?
The code is this, I have done some testing but the original image and thumbnail are loaded properly in the directory, but all the data are not passed to the database.
What is the correct position to insert the code?
Thank you
function imgUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){
//folder path setup
$target_path = $target_folder;
$thumb_path = $thumb_folder;
//file name setup
$upload_image = $_FILES[$field_name]["name"];
$tmpName = $_FILES[$field_name]['tmp_name'];
$size = $_FILES[$field_name]['size'];
$ext = strtolower(pathinfo($upload_image, PATHINFO_EXTENSION));
$fileName = rand(10000,1000000).".".$ext;
//upload image path
$upload_image = $target_path.basename($fileName);
//upload image
if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
{
//thumbnail creation
if($thumb == TRUE)
{
$thumbnail = $thumb_path.$fileName;
list($width,$height) = getimagesize($upload_image);
$thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
switch($file_ext){
case 'jpg':
$source = imagecreatefromjpeg($upload_image);
break;
case 'jpeg':
$source = imagecreatefromjpeg($upload_image);
break;
case 'png':
$source = imagecreatefrompng($upload_image);
break;
case 'gif':
$source = imagecreatefromgif($upload_image);
break;
default:
$source = imagecreatefromjpeg($upload_image);
}
imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
switch($file_ext){
case 'jpg' || 'jpeg':
imagejpeg($thumb_create,$thumbnail,100);
break;
case 'png':
imagepng($thumb_create,$thumbnail,100);
break;
case 'gif':
imagegif($thumb_create,$thumbnail,100);
break;
default:
imagejpeg($thumb_create,$thumbnail,100);
}
}
return $fileName;
}
else
{
return false;
}
}
include('includes/Config.php'); //database connection
$pdo = new db();
$date = date("F j, Y");
$title = $_POST['title'];
$description = $_POST['description'];
if(!empty($_FILES['image']['name'])){
//call thumbnail creation function and store thumbnail name
$upload_img = imgUpload('image','uploads/','',TRUE,'uploads/thumbs/','260','260');
//full path of the thumbnail image
$thumb_src = 'uploads/thumbs/'.$upload_img;
//set success and error messages
$message = $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";
// After that you can insert the path of the resized image into the database
$pdo->query('INSERT INTO simple_gallery (title, description, photo, date) VALUES (:title, :description, :photo, :date)');
$pdo->bind(':title', $title);
$pdo->bind(':description', $description);
$pdo->bind(':photo', $fileName);
$pdo->bind(':date', $date);
$pdo->execute();
}else{
//if form is not submitted, below variable should be blank
$thumb_src = '';
$message = '';
}
And the form:
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<table class="table table-bordered table-responsive">
<tr>
<td>
<label class="control-label">Title.
</label>
</td>
<td>
<input class="form-control" type="text" name="title" placeholder="Enter Username" value="<?php echo $title; ?>" />
</td>
</tr>
<tr>
<td>
<label class="control-label">Description.
</label>
</td>
<td>
<input class="form-control" type="text" name="description" placeholder="Your Profession" value="<?php echo $description; ?>" />
</td>
</tr>
<tr>
<td>
<label class="control-label">Profile Img.
</label>
</td>
<td>
<input class="input-group" type="file" name="image"/>
</td>
</tr>
<tr>
<td colspan="2">
<button type="submit" name="submit" class="btn btn-default">
<span class="glyphicon glyphicon-save">
</span> save
</button>
</td>
</tr>
</table>
</form>