将配置文件图片上载一次(不是两次)到数据库中,并正确显示它们

时间:2017-06-15 21:00:44

标签: php mysql image mysqli

我正在尝试在我的网站上构建一个用户可以上传个人资料图片的选项,到目前为止一直很好,但是当我刷新页面时,数据会一次又一次地保存在我的数据库(表格)中

++图像没有显示,它只显示一个黑色方块。 感谢您的帮助,正如您所看到的,我仍然是一个绝对的初学者,热切而凶猛地学习

$images = (isset ($_FILES['images']['name']));
$paths= [];


if ($images)
{
    $saveto = "uploads/$user.jpg";
    move_uploaded_file($_FILES['images']['tmp_name'], $saveto);
    $typeok = TRUE;
    $paths[] = $saveto;
    $date1=date("Y-m-d h:m:s");

    $link=mysqli_connect("localhost","root","","vlindr");
    mysqli_select_db($link,"imageupload"); //database connectivity

    mysqli_query($link,"insert into images 
                                (images_id, images_path, submission_date) 
                        VALUES (NULL,'$user','$date1')")
        or die(mysqli_error($link));

    switch($_FILES['images']['type'])
    {
        case "image/gif":   $src = imagecreatefromgif($saveto); break;
        case "image/jpeg":  // Both regular and progressive jpegs
        case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
        case "image/png":   $src = imagecreatefrompng($saveto); break;
        default:            $typeok = FALSE; break;
    }

    if ($typeok)
    {
        list($w, $h) = getimagesize($saveto);

        $max = 150;
        $tw  = $w;
        $th  = $h;

        if ($w > $h && $max < $w) {
            $th = $max ;
            $tw = $max;
        }
        elseif ($h > $w && $max < $h) {
            $tw = $max / $h * $w;
            $th = $max;
        }
        elseif ($max < $w)           {
            $tw = $th = $max;
        }

        $tmp = imagecreatetruecolor($tw, $th);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
        imageconvolution($tmp,  array(array(-1, -1, -1),
                                array(-1, 16, -1), 
                                array(-1, -1, -1)),
                         8, 0);
        imagejpeg($tmp, $saveto);
        imagedestroy($tmp);
        imagedestroy($src);
    }
}

1 个答案:

答案 0 :(得分:0)

如果以下代码有效,请与我们联系。 我也推荐预备语句,虽然下面的代码不是最好的,它可以完成工作!

$images = (isset ($_FILES['images']['name']));
$paths= [];


if ($images)
{
$saveto = "uploads/$user.jpg";
move_uploaded_file($_FILES['images']['tmp_name'], $saveto);
$typeok = TRUE;
$paths[] = $saveto;
$date1=date("Y-m-d h:m:s");

$link=mysqli_connect("localhost","root","","vlindr");
mysqli_select_db($link,"imageupload"); //database connectivity

//Checks if Image already exists in database
$stmt = $link->prepare("SELECT * FROM images WHERE images_path=(?)");
$stmt->bind_param("s",$saveto);
$stmt->execute();
$stmt->store_result();
//If the amount of rows returned is zero, the script will insert the image into the database
if($stmt->num_rows === 0) {

    //Inserts the image into the database
    $stmt2 = $link->prepare("INSERT INTO images (images_id,images_path,submission_date) VALUES (NULL,?,?)");
    $stmt2->bind_param("ss",$saveto,$date1);
    if($stmt2->execute()) {
        echo "Image is unique and uploaded to database";
    }

    switch($_FILES['images']['type'])
    {
        case "image/gif":   $src = imagecreatefromgif($saveto); break;
        case "image/jpeg":  // Both regular and progressive jpegs
        case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
        case "image/png":   $src = imagecreatefrompng($saveto); break;
        default:            $typeok = FALSE; break;
    }

    if ($typeok)
    {
        list($w, $h) = getimagesize($saveto);

        $max = 150;
        $tw  = $w;
        $th  = $h;

        if ($w > $h && $max < $w)
        {
            $th = $max ;
            $tw = $max;
        }
        elseif ($h > $w && $max < $h)
        {
            $tw = $max / $h * $w;
            $th = $max;
        }
        elseif ($max < $w)
        {
            $tw = $th = $max;
        }

        $tmp = imagecreatetruecolor($tw, $th);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
        imageconvolution($tmp, array(array(-1, -1, -1),
            array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
        imagejpeg($tmp, $saveto);
        imagedestroy($tmp);
        imagedestroy($src);

    }

}//end if
else {
    echo "Image Already Exists";
}//end else