Php 上传图像
我已经完成上传图像。代码工作得很完美。我想在将图像保存到数据库时插入会话id。如何在insert query.view文件中保存会话id以在库中显示图像。我想将它们显示为用户 这就是为什么我需要保存用户id以检索特定用户的特定图像。现在它正在从数据库中获取所有图像。我想存储用户id以跟踪特定图像。在这里,我开始了我的课程。这是我的会话idPhp 上传图像,php,mysql,gallery,Php,Mysql,Gallery,我已经完成上传图像。代码工作得很完美。我想在将图像保存到数据库时插入会话id。如何在insert query.view文件中保存会话id以在库中显示图像。我想将它们显示为用户 这就是为什么我需要保存用户id以检索特定用户的特定图像。现在它正在从数据库中获取所有图像。我想存储用户id以跟踪特定图像。在这里,我开始了我的课程。这是我的会话id <?php session_start(); //what can be done to save this id.
<?php
session_start(); //what can be done to save this id.I need save this staffid to save in the database.What is the process
if(!$this->session->userdata('staffid')){ redirect("login/checkLogin");
}
?>
<?php
好的,首先你需要理解会话
如果我理解正确:
- 您希望将当前用户的链接保存到图像(保存在会话中),然后
- 拉取用户在后期保存的图像
这是正确的吗
指出“staffid”是独一无二的:
在图像表上创建列:
更改表格tbl_图像
ADDstaffid
INT NOT NULL
然后将insert语句更改为
$sql=“插入tbl_图像(图像名称,staffid)值(:img,:staffid)”
及
$stmt->bindValue($img“,$filename);
$stmt->bindValue(“:staffid”,$this->session->userdata('staffid')
然后,当选择仅需要传入当前用户id时,添加where子句并相应地准备查询。这将返回该用户保存的所有图像:
其中staffid=:staffid
您首先需要编写清晰易读的代码
if (isset($_POST["sub1"]) || isset($_POST["sub2"])) {
// include resized library
require_once('./php-image-magician/php_image_magician.php'); //library for showing images in thumbnail.This library is helping for showing images as gallery.
$msg = "";
$valid_image_check = array("image/gif", "image/jpeg", "image/jpg", "image/png", "image/bmp");
if (count($_FILES["user_files"]) > 0) {
$folderName = "uploads/";
$sql = "INSERT INTO tbl_images(image_name) VALUES (:img)"; //have insert my session id here.What is the procedure to insert session id in sql insert query.
$stmt = $DB->prepare($sql);
for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) { //Here is the loop for saving multiple images in database.
if ($_FILES["user_files"]["name"][$i] <> "") {
$image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["user_files"]["tmp_name"][$i])));
// if valid image type then upload
if (in_array($image_mime, $valid_image_check)) {
$ext = explode("/", strtolower($image_mime));
$ext = strtolower(end($ext));
$filename = rand(10000, 990000) . '_' . time() . '.' . $ext;
$filepath = $folderName . $filename;
//this will save the folder name and file path in database.There is no problem saving them.Working successfully.
if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filepath)) {
$emsg .= "Failed to upload <strong>" . $_FILES["user_files"]["name"][$i] . "</strong>. <br>";
$counter++;
} else {
$smsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> uploaded successfully. <br>";
$magicianObj = new imageLib($filepath);
$magicianObj->resizeImage(100, 100);
$magicianObj->saveImage($folderName . 'thumb/' . $filename, 100);
/* * ****** insert into database starts ******** */
try {
$stmt->bindValue(":img", $filename); //i have to insert my session id in here.
$stmt->execute();
$result = $stmt->rowCount();
if ($result > 0) {
// file uplaoded successfully.
} else {
// failed to insert into database.
}
} catch (Exception $ex) {
$emsg .= "<strong>" . $ex->getMessage() . "</strong>. <br>";
}
/* * ****** insert into database ends ******** */
}
} else {
$emsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> not a valid image. <br>";
//After checking file type if anything goes wrong it will show this message.File type is given.
}
}
}
$msg .= (strlen($smsg) > 0) ? successMessage($smsg) : "";
$msg .= (strlen($emsg) > 0) ? errorMessage($emsg) : "";
} else {
$msg = errorMessage("You must upload atleast one file");//validation for uploading files.
}
}
?>