douyun8901 2014-07-19 21:46
浏览 53
已采纳

使用最近上传的图像在PHP中更新数据库

I am trying to get the filename of an uploaded file so I can update a database, here is what I have at the moment. Using image Maniuplator

https://gist.github.com/philBrown/880506

<?php
require_once(dirname(__FILE__).'/../config.php');
require_once(dirname(__FILE__).'/ImageManipulator.php');

if ($_FILES['fileToUpload']['error'] > 0) {
    echo "Error: " . $_FILES['fileToUpload']['error'] . "<br />";
} else {
    // array of valid extensions
    $validExtensions = array('.jpg', '.jpeg', '.gif', '.png', '.JPG', '.JPEG', '.PNG', '.GIF', '.bmp');
    // get extension of the uploaded file
    $fileExtension = strrchr($_FILES['fileToUpload']['name'], ".");
    // check if file Extension is on the list of allowed ones
    if (in_array($fileExtension, $validExtensions)) {
        $newNamePrefix = time() . '_';
        $manipulator = new ImageManipulator($_FILES['fileToUpload']['tmp_name']);
        $width  = $manipulator->getWidth();
        $height = $manipulator->getHeight();
        $centreX = round($width / 2);
        $centreY = round($height / 2);
        // our dimensions will be 80by80
        $x1 = $centreX - 40; // 80
        $y1 = $centreY - 40; // 80

        $x2 = $centreX + 40; // 80
        $y2 = $centreY + 40; // 80

        // center cropping to 80by80
        $newImage = $manipulator->crop($x1, $y1, $x2, $y2);
        // saving file to uploads folder
        $manipulator->save('../images/avatars/uploaded/' . $newNamePrefix . $_FILES['fileToUpload']['name']);
        $user_image_1 = '$newNamePrefix';
        $user_image_2 = $_FILES['fileToUpload']['name'];
        $user_image_file = "{$domain}/images/avatars/uploaded/{$user_image_1}{$user_image_2}";
        // Update the DB with new avatar
            function set_new_avatar() {
                global $con, $user_id, $user_image_file;
                $sql = "UPDATE users SET user_image='$user_image_file' WHERE id='$user_id'";
                mysqli_query($con, $sql);
            }
        set_new_avatar();
        header('Location: ../index.php');

    } else {
        echo 'You must upload an image...';
    }
}
?> 

I have tried wrapping, $user_image_2 in (", also tried wrapping the variable in {} but nothing seems to work.

The file is uploaded and the database is being updated with [domain/images/avatars/uploaded/], but the problem is as it's being renamed during the upload and I can't capture the name.

I imagine it's a small syntax issue but I can't get my head around how to sort it out.

Thank you in advanced

  • 写回答

1条回答 默认 最新

  • drau89457 2014-07-20 01:18
    关注

    There were 2 problems here, 1. User_image_1 was being passed as a variable and not a string.

    $user_image_1 = '$newNamePrefix';
    

    needed to be

    $user_image_1 = $newNamePrefix;
    

    The second was that user_id wasn't properly being passed to this screen and therefor it was blank.

    I managed to find this out, by printing different aspects of the code rather than redirecting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测