将id插入MYSQL表以进行多个图像上传

时间:2014-09-09 08:27:26

标签: php jquery mysql ajax upload

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

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

<?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);
}

1 个答案:

答案 0 :(得分:0)

希望这有帮助。

请在每个文件的顶部使用提取($ _ REQUEST)。此函数使post和get变量成为全局变量,如果包含该文件或调用另一个文件,则可以使用该变量。 然后回显 $ cid

对于使用此插入函数。

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