gpt4 book ai didi

php - 将id插入MYSQL表以进行多图片上传

转载 作者:行者123 更新时间:2023-11-29 23:49:09 24 4
gpt4 key购买 nike

我目前有一个表单,可以在数据库中创建一个项目,然后在下一步中,它允许用户为该列表上传多个图像,这两个部分都可以独立工作,但我需要能够从进入图像上传查询的第一页,以确定其对应的列表

这是允许用户上传多张图片的页面

<?php require_once 'config.php';

// Start the session
session_start();

// Require the classes for the page
require_once '../../assets/imageClass.php';

// create a new object class
$objects = new imageClass();

// set page variables
$path = '../';
$title = 'image upload';
$replace = 'content';
$message = '';



// Connect to the database


if(isset($_POST['image_upload'])){

// Filter all of the $_POST data
$objects->filterEverything($_POST);

// Declare shorthand for the id value if there is $_POST data
$cid = $objects->clean['cid'];
}
else{
// Filter all of the $_GET data
$objects->filterEverything($_GET);

// Declare shorthand for the id value if there is $_GET data
$cid = $objects->clean['cid'];
}


?>
<!DOCTYPE html>
<html>
<head>
<title>Autoways Image Upload</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='http://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/style.css" >

</head>
<body>
<div class="container">
<h1 class="page-title" >Upload images</h1>
<div class="form-container">
<form enctype="multipart/form-data" name='imageform' role="form" id="imageform" method="post" action="ajax.php">
<div class="form-group">
<p>Please Choose Image: </p>
<input class='file' multiple="multiple" type="file" class="form-control" name="images[]" id="images" placeholder="Please choose your image">
<input type="hidden" name="cid" value="<?php echo $cid;?>" />
<span class="help-block"></span>
</div>
<div id="loader" style="display: none;">
Please wait image uploading to server....
</div>
<input type="submit" value="Upload" name="image_upload" id="image_upload" class="btn"/>
</form>
</div>
<div class="clearfix"></div>
<div id="uploaded_images" class="uploaded-images">
<div id="error_div">
</div>
<div id="success_div">
</div>

</div>

<a href="../index.php">I have finished uploading images</a>
<?php echo $cid; ?>

</div>
<input type="hidden" id='base_path' value="<?php echo BASE_PATH; ?>">
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.form.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>

这里是上传功能

<?php
require_once 'config.php';
$data = array();
if( isset( $_POST['image_upload'] ) && !empty( $_FILES['images'] )){
//get the structured array
$images = restructure_array( $_FILES );
$allowedExts = array("gif", "jpeg", "jpg", "png");

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}


foreach ( $images as $key => $value){
$i = $key+1;
//create directory if not exists
if (!file_exists('images')) {
mkdir('images', 0777, true);
}
$image_name = $value['name'];
//get image extension
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
//assign unique name to image
$name = $i*time().'.'.$ext;
//$name = $image_name;
//image size calcuation in KB
$image_size = $value["size"] / 1024;
$image_flag = true;
//max image size
$max_size = 512;
if( in_array($ext, $allowedExts) && $image_size < $max_size ){
$image_flag = true;
} else {
$image_flag = false;
$data[$i]['error'] = 'Maybe '.$image_name. ' exceeds max '.$max_size.' KB size or incorrect file extension';
}

if( $value["error"] > 0 ){
$image_flag = false;
$data[$i]['error'] = '';
$data[$i]['error'].= '<br/> '.$image_name.' Image contains error - Error Code : '.$value["error"];
}

if($image_flag){
move_uploaded_file($value["tmp_name"], "images/".$name);
$src = "images/".$name;
$dist = "images/thumbnail_".$name;
$data[$i]['success'] = $thumbnail = 'thumbnail_'.$name;

thumbnail($src, $dist, 200);
$sql="INSERT INTO images (`id`, `original_image`, `thumbnail_image`, `ip_address`) VALUES (NULL, '$name', '$thumbnail', '$ip');";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}



}
}
mysqli_close($con);
echo json_encode($data);

} else {
$data[] = 'No Image Selected..';
}



function restructure_array(array $images)
{
$result = array();

foreach ($images as $key => $value) {
foreach ($value as $k => $val) {
for ($i = 0; $i < count($val); $i++) {
$result[$i][$k] = $val[$i];
}
}
}

return $result;
}



function thumbnail($src, $dist, $dis_width = 100 ){

$img = '';
$extension = strtolower(strrchr($src, '.'));
switch($extension)
{
case '.jpg':
case '.jpeg':
$img = @imagecreatefromjpeg($src);
break;
case '.gif':
$img = @imagecreatefromgif($src);
break;
case '.png':
$img = @imagecreatefrompng($src);
break;
}
$width = imagesx($img);
$height = imagesy($img);




$dis_height = $dis_width * ($height / $width);

$new_image = imagecreatetruecolor($dis_width, $dis_height);
imagecopyresampled($new_image, $img, 0, 0, 0, 0, $dis_width, $dis_height, $width, $height);


$imageQuality = 100;

switch($extension)
{
case '.jpg':
case '.jpeg':
if (imagetypes() & IMG_JPG) {
imagejpeg($new_image, $dist, $imageQuality);
}
break;

case '.gif':
if (imagetypes() & IMG_GIF) {
imagegif($new_image, $dist);
}
break;

case '.png':
$scaleQuality = round(($imageQuality/100) * 9);
$invertScaleQuality = 9 - $scaleQuality;

if (imagetypes() & IMG_PNG) {
imagepng($new_image, $dist, $invertScaleQuality);
}
break;
}
imagedestroy($new_image);
}

最佳答案

希望这有帮助。

请在每个文件的顶部使用extract($_REQUEST)。此函数将变量发布并获取到全局变量,如果您包含该文件或调用另一个文件,则可以使用这些全局变量。然后回显$cid

对于插入功能,使用了这个。

$sql="INSERT INTO images (`id`,`listing`,`original_image`,`thumbnail_image`,`ip_address`) VALUES (NULL, $cid, '$name', '$thumbnail', '$ip');";
mysql_query($sql);

关于php - 将id插入MYSQL表以进行多图片上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25739904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com