Php 如何上传图像并将数据插入MySQL数据库

Php 如何上传图像并将数据插入MySQL数据库,php,mysql,pdo,Php,Mysql,Pdo,我在“addcategory.PHP”上有一个PHP表单,它允许管理员添加一个产品类别,并在这个过程中将一个图像上传到一个预定的文件夹,并将其文件名和相关数据插入数据库。该脚本在mysql上运行良好,但在我将“functions.php”升级到PDO后,它就停止了工作。主要问题是添加类别失败,因为图像无法上载。帮我修复它,这样图像就可以上传了。我想包括PDO准备的语句的安全好处 这是错误消息: Warning: imagejpeg(/home/ script-directory/products

我在“addcategory.PHP”上有一个PHP表单,它允许管理员添加一个产品类别,并在这个过程中将一个图像上传到一个预定的文件夹,并将其文件名和相关数据插入数据库。该脚本在mysql上运行良好,但在我将“functions.php”升级到PDO后,它就停止了工作。主要问题是添加类别失败,因为图像无法上载。帮我修复它,这样图像就可以上传了。我想包括PDO准备的语句的安全好处

这是错误消息:

Warning: imagejpeg(/home/ script-directory/products/images/category/ec27a92192042fdc049e54477649fb30.jpg): failed to open stream: No such file or directory in /home/script-directory/includes/functions.php on line 385
以下是“add category.php”:

它输出“无文件或目录错误”和此代码,其中包含我在表单中输入的数据:

array (size=6)
  ':categoryName' => string 'Balls' (length=5)
  ':categoryMtitle' => string 'Corporate Gift balls' (length=20)
  ':categoryMkey' => string 'ball, balls' (length=11)
  ':categoryMdes' => string 'Buy corporate gift items like balls' (length=35)
  ':categoryDesc' => string '<p>Buy corporate gift items like balls</p>' (length=42)
  ':newName' => string '8da973c4e2b0dd801d55c3dae56f89d7.jpg' (length=36)
在“functions.php”中,我有关于PDO和图像上传的代码:

    /**
    * Executes SQL statement, possibly with parameters, returning
    * a pdo statement object on success, handling and halting execution on error.
    */
    function query($sql, $parameters = null)
    {
        static $pdo; // define the var as static so that it will persist between function calls
    try
    {
        // if no db connection, make one
        if (!isset($pdo))
        {
            // connect to database
            // you should set the character encoding for the connection
            $pdo = new PDO("mysql:dbname=" . DB_NAME . ";host=" . DB_SERVER, DB_USERNAME, DB_PASSWORD);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the error mode to exceptions
            $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); // turn emulated prepares off
            $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); // set default fetch mode to assoc so that you don't have to explicitly list the fetch mode every place
        }
        if(empty($parameters)){
            // no bound inputs
            $stmt = $pdo->query($sql);
        } else {
            // has bound inputs
            $stmt = $pdo->prepare($sql);
             // you should use explicit bindValue() statements to bind any inputs, instead of supplying them as a parameter to the ->execute() method. the comments posted in your thread lists the reasons why.
            $stmt->execute($parameters);
        }
        }
        catch (Exception $e)
        {
            // all errors with the connection, query, prepare, and execute will be handled here
            // you should also use the line, file, and backtrace information to produce a detailed error message
            // if the error is due to a query, you should also include the $sql statement as part of the error message
            // if $pdo ($handle in your code) is set, it means that the connection was successful and the error is due to a query. you can use this to include the $sql in the error message.
            trigger_error($e->getMessage(), E_USER_ERROR);
            //exit; // note: E_USER_ERROR causes an exit, so you don't need an exit; here.
        }
            return $stmt; // if the query ran without any errors, return the pdo statement object to the calling code
        }
/*
    Upload an image and create the thumbnail. The thumbnail is stored 
    under the thumbnail sub-directory of $uploadDir.
    Return the uploaded image name and the thumbnail also.
*/
function uploadImage($inputName, $uploadDir)
{
    $image     = $_FILES[$inputName];
    $imagePath = '';
    $thumbnailPath = '';
    // if a file is given
    if (trim($image['tmp_name']) != '') {
        $ext = substr(strrchr($image['name'], "."), 1); 
        // generate a random new file name to avoid name conflict
        // then save the image under the new file name
        $imagePath = md5(rand() * time()) . ".$ext";
        $result    = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
        if ($result) {
            // create thumbnail
            $thumbnailPath =  md5(rand() * time()) . ".$ext";
            $result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);
            // create thumbnail failed, delete the image
            if (!$result) {
                unlink($uploadDir . $imagePath);
                $imagePath = $thumbnailPath = '';
            } else {
                $thumbnailPath = $result;
            }   
        } else {
            // the image cannot be uploaded
            $imagePath = $thumbnailPath = '';
        }
    }
    return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
/*
    Create a thumbnail of $srcFile and save it to $destFile.
    The thumbnail will be $width pixels.
*/
function createThumbnail($srcFile, $destFile, $width, $quality = 75)
{
    $thumbnail = '';
    if (file_exists($srcFile)  && isset($destFile))
    {
        $size        = getimagesize($srcFile);
        $w           = number_format($width, 0, ',', '');
        $h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
        $thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
    }
    // return the thumbnail file name on sucess or blank on fail
    return basename($thumbnail);
}
/*
    Copy an image to a destination file. The destination
    image size will be $w X $h pixels
*/
function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
{
    $tmpSrc     = pathinfo(strtolower($srcFile));
    $tmpDest    = pathinfo(strtolower($destFile));
    $size       = getimagesize($srcFile);
    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
    {
       $destFile  = substr_replace($destFile, 'jpg', -3);
       $dest      = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } elseif ($tmpDest['extension'] == "png") {
       $dest = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } else {
      return false;
    }
    switch($size[2])
    {
       case 1:       //GIF
           $src = imagecreatefromgif($srcFile);
           break;
       case 2:       //JPEG
           $src = imagecreatefromjpeg($srcFile);
           break;
       case 3:       //PNG
           $src = imagecreatefrompng($srcFile);
           break;
       default:
           return false;
           break;
    }
    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
    switch($size[2])
    {
       case 1:
       case 2:
           imagejpeg($dest,$destFile, $quality);
           break;
       case 3:
           imagepng($dest,$destFile);
    }
    return $destFile;
}

您在哪里调用
uploadImage($inputName,$uploadDir){
?我也想知道这一点。我只能看到
create缩略图()
显式调用。我不确定我是否完全理解您的问题。upload类是几年前由某人编写的,在升级到PDO之前它在我的脚本中工作。您是说从“add category.php”中调用uploadImage($inputName,$uploadDir){吗?您希望在哪里看到它?在哪里调用
uploadImage($inputName,$uploadDir){
?我也想知道这一点。我只能看到显式调用了
createThumbnail()
。我不确定我是否完全理解你的问题。upload类是几年前由某人编写的,它在我的脚本上工作,就像升级到PDO之前一样。你的意思是调用uploadImage($inputName,$uploadDir){在“addcategory.php”中?您希望在哪里看到它?
array (size=6)
  ':categoryName' => string 'Balls' (length=5)
  ':categoryMtitle' => string 'Corporate Gift balls' (length=20)
  ':categoryMkey' => string 'ball, balls' (length=11)
  ':categoryMdes' => string 'Buy corporate gift items like balls' (length=35)
  ':categoryDesc' => string '<p>Buy corporate gift items like balls</p>' (length=42)
  ':newName' => string '8da973c4e2b0dd801d55c3dae56f89d7.jpg' (length=36)
// APP_ROOT will always be set to 2 directorys up from the location of this file
    define('APP_ROOT', dirname(dirname(__FILE__)) . '/');
    // a category can have an image used as thumbnail
    // we save the category image here
    define('ALBUM_IMG_DIR', APP_ROOT . 'products/images/category/');
// all images inside an category are stored here
define('GALLERY_IMG_DIR', APP_ROOT . 'products/images/gallery/');
    /**
    * Executes SQL statement, possibly with parameters, returning
    * a pdo statement object on success, handling and halting execution on error.
    */
    function query($sql, $parameters = null)
    {
        static $pdo; // define the var as static so that it will persist between function calls
    try
    {
        // if no db connection, make one
        if (!isset($pdo))
        {
            // connect to database
            // you should set the character encoding for the connection
            $pdo = new PDO("mysql:dbname=" . DB_NAME . ";host=" . DB_SERVER, DB_USERNAME, DB_PASSWORD);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the error mode to exceptions
            $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); // turn emulated prepares off
            $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); // set default fetch mode to assoc so that you don't have to explicitly list the fetch mode every place
        }
        if(empty($parameters)){
            // no bound inputs
            $stmt = $pdo->query($sql);
        } else {
            // has bound inputs
            $stmt = $pdo->prepare($sql);
             // you should use explicit bindValue() statements to bind any inputs, instead of supplying them as a parameter to the ->execute() method. the comments posted in your thread lists the reasons why.
            $stmt->execute($parameters);
        }
        }
        catch (Exception $e)
        {
            // all errors with the connection, query, prepare, and execute will be handled here
            // you should also use the line, file, and backtrace information to produce a detailed error message
            // if the error is due to a query, you should also include the $sql statement as part of the error message
            // if $pdo ($handle in your code) is set, it means that the connection was successful and the error is due to a query. you can use this to include the $sql in the error message.
            trigger_error($e->getMessage(), E_USER_ERROR);
            //exit; // note: E_USER_ERROR causes an exit, so you don't need an exit; here.
        }
            return $stmt; // if the query ran without any errors, return the pdo statement object to the calling code
        }
/*
    Upload an image and create the thumbnail. The thumbnail is stored 
    under the thumbnail sub-directory of $uploadDir.
    Return the uploaded image name and the thumbnail also.
*/
function uploadImage($inputName, $uploadDir)
{
    $image     = $_FILES[$inputName];
    $imagePath = '';
    $thumbnailPath = '';
    // if a file is given
    if (trim($image['tmp_name']) != '') {
        $ext = substr(strrchr($image['name'], "."), 1); 
        // generate a random new file name to avoid name conflict
        // then save the image under the new file name
        $imagePath = md5(rand() * time()) . ".$ext";
        $result    = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
        if ($result) {
            // create thumbnail
            $thumbnailPath =  md5(rand() * time()) . ".$ext";
            $result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);
            // create thumbnail failed, delete the image
            if (!$result) {
                unlink($uploadDir . $imagePath);
                $imagePath = $thumbnailPath = '';
            } else {
                $thumbnailPath = $result;
            }   
        } else {
            // the image cannot be uploaded
            $imagePath = $thumbnailPath = '';
        }
    }
    return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
/*
    Create a thumbnail of $srcFile and save it to $destFile.
    The thumbnail will be $width pixels.
*/
function createThumbnail($srcFile, $destFile, $width, $quality = 75)
{
    $thumbnail = '';
    if (file_exists($srcFile)  && isset($destFile))
    {
        $size        = getimagesize($srcFile);
        $w           = number_format($width, 0, ',', '');
        $h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
        $thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
    }
    // return the thumbnail file name on sucess or blank on fail
    return basename($thumbnail);
}
/*
    Copy an image to a destination file. The destination
    image size will be $w X $h pixels
*/
function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
{
    $tmpSrc     = pathinfo(strtolower($srcFile));
    $tmpDest    = pathinfo(strtolower($destFile));
    $size       = getimagesize($srcFile);
    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
    {
       $destFile  = substr_replace($destFile, 'jpg', -3);
       $dest      = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } elseif ($tmpDest['extension'] == "png") {
       $dest = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } else {
      return false;
    }
    switch($size[2])
    {
       case 1:       //GIF
           $src = imagecreatefromgif($srcFile);
           break;
       case 2:       //JPEG
           $src = imagecreatefromjpeg($srcFile);
           break;
       case 3:       //PNG
           $src = imagecreatefrompng($srcFile);
           break;
       default:
           return false;
           break;
    }
    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
    switch($size[2])
    {
       case 1:
       case 2:
           imagejpeg($dest,$destFile, $quality);
           break;
       case 3:
           imagepng($dest,$destFile);
    }
    return $destFile;
}