繁体   English   中英

如何在MySQL数据库中保存上传的图像名称

[英]How to save uploaded image names in MySQL database

此回答问题的一部分

我有下面的PHP代码,将一个裁剪的图像上传到3个不同的宽度和高度

  1. 头像目录,宽度和高度200 * 200
  2. 宽度和高度为500 * 500的avatar1目录
  3. 宽度和高度700 * 700的avatar2目录

并使用MD5 random重命名图像。

<?php 
    function uploadImageFile() { // Note: GD library is required for this function

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $iJpgQuality = 100;

        if ($_FILES) {
            // if no errors and size less than 250kb
            if (! $_FILES['image_file']['error'] && $_FILES['image_file']['size'] < 250 * 1024) {
                if (is_uploaded_file($_FILES['image_file']['tmp_name'])) {
                    if (!is_dir('avatar')) {
                        mkdir('avatar');
                    }
                    // new unique filename
                    $sTempFileName = 'avatar/' . md5(time().rand());
                    // move uploaded file into cache folder
                    move_uploaded_file($_FILES['image_file']['tmp_name'], $sTempFileName);

                    // change file permission to 644
                    @chmod($sTempFileName, 0644);

                    $sResultFileName = copyImageFile('avatar', $sTempFileName, 200, 200, $iJpgQuality);
                    if ($sResultFileName) {
                        copyImageFile('avatar1', $sTempFileName, 500, 500);
                        copyImageFile('avatar2', $sTempFileName, 700, 700);
                        @unlink($sTempFileName);

                        return $sResultFileName;
                    }
                }
            }
        }
    }
    return false;
}

function copyImageFile($dirName, $originImageName, $iWidth, $iHeight, $iJpgQuality = 90) {
    if (file_exists($originImageName) && filesize($originImageName) > 0) {        
        $aSize = getimagesize($originImageName); // try to obtain image info
        if (!$aSize) {
            @unlink($originImageName);
            return;
        }

        // check for image type
        switch($aSize[2]) {
            case IMAGETYPE_JPEG:
                $sExt = '.jpg';
                $vImg = @imagecreatefromjpeg($originImageName);
                break;
            /*case IMAGETYPE_GIF:
                $sExt = '.gif';

                // create a new image from file 
                $vImg = @imagecreatefromgif($sTempFileName);
                break;*/
            case IMAGETYPE_PNG:
                $sExt = '.png';
                $vImg = @imagecreatefrompng($originImageName);
                break;
            default:
                @unlink($originImageName);
                return;
        }

        // create a new true color image
        $vDstImg = @imagecreatetruecolor( $iWidth, $iHeight );

        // copy and resize part of an image with resampling
        imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']);

        // define a result image filename        
        if (!is_dir($dirName)) {
            mkdir($dirName);
        }
        $newImageName = $dirName . DIRECTORY_SEPARATOR . md5(time().rand()) . $sExt;

        // output image to file
        imagejpeg($vDstImg, $newImageName, $iJpgQuality);
        //@unlink($sTempFileName);

        return $newImageName;
    }

    return false;
}

$sImage = uploadImageFile();
echo '<img src="'.$sImage.'" />';
?>

我的问题

我想将最近上传的3张图片的URL保存在ara MySQL数据库中

  1. Avatar列=上传到头像目录的图片的URL
  2. Avatar1列=上传到avatar1目录的图像的URL
  3. Avatar2列=上传到avatar2目录的图像的URL

数据库名称:ara

数据库用户:root

数据库密码:

数据库地址:127.0.0.1

表名称:配置文件

列名称:头像,头像1和头像2

copyImageFile('avatar1', $sTempFileName, 500, 500);
copyImageFile('avatar2', $sTempFileName, 700, 700);

之后替换此代码

     include('connect/mysql.php');
    $avatar1=copyImageFile('avatar1', $sTempFileName, 500, 500);
    $avatar2=copyImageFile('avatar2', $sTempFileName, 700, 700);
$user_name = mysqli_real_escape_string($con, $_SESSION['UserName']);

       mysqli_query($con,"UPDATE profiles SET AvatarImage='".$sResultFileName."',AvatarImageBig='".$avatar1."',AvatarImageSmall='".$avatar2."' WHERE UserName = '$user_name'");   
        mysqli_close($con);

感谢@Banjamin和@Qaisar

您在函数中使用$ con

 uploadImageFile($user_name), 

因此您应该将变量$con传递给此函数,例如

uploadImageFile($user_name, $con)当然,您必须更改函数接受的参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM