图片文件路径无法使用PHP上传到MySQL数据库

ssm49v7z  于 2023-11-16  发布在  Mysql
关注(0)|答案(1)|浏览(132)

我使用PHP,我试图上传MySQL数据库中的图像文件路径,但路径无法上传,它总是显示为空我的代码如下所示。

if(isset($_FILES['img']) && $_FILES['img']['tmp_name'] != ''){
                    $fname = 'uploads/avatar-'.($id).'.png';
                    $dir_path =base_app. $fname;
                    $upload = $_FILES['img']['tmp_name'];
                    $type = mime_content_type($upload);
                    $allowed = array('image/png','image/jpeg');
                    if(!in_array($type,$allowed)){
                        $resp['msg']=" But Image failed to upload due to invalid file type.";
                    }else{
                        $new_height = 200; 
                        $new_width = 200; 
                
                        list($width, $height) = getimagesize($upload);
                        $t_image = imagecreatetruecolor($new_width, $new_height);
                        imagealphablending( $t_image, false );
                        imagesavealpha( $t_image, true );
                        $gdImg = ($type == 'image/png')? imagecreatefrompng($upload) : imagecreatefromjpeg($upload);
                        imagecopyresampled($t_image, $gdImg, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                        if($gdImg){
                                if(is_file($dir_path))
                                unlink($dir_path);
                                $uploaded_img = imagepng($t_image,$dir_path);
                                imagedestroy($gdImg);
                                imagedestroy($t_image);
                                if(isset($uploaded_img) && $uploaded_img == true){
                                    $qry = $this->conn->query("UPDATE users set avatar = concat('{$fname}','?v=',unix_timestamp(CURRENT_TIMESTAMP)) where id = '$id' ");
                                }
                        }else{
                        $resp['msg']=" But Image failed to upload due to unkown reason.";
                        }
                    }
                    
                }

字符串
我要上传图片文件路径并显示图片

sshcrbum

sshcrbum1#

根据提供的代码,您似乎正在尝试上传图像并将其路径保存到MySQL数据库中。如果路径在数据库中显示为NULL,可能有几个原因。请参阅php framework list的此资源。以下是解决此问题的一些建议:
1.目录权限:确保uploads目录具有必要的写权限,Web服务器需要在该目录下创建和写入文件的权限。
示例(Linux):

chmod -R 755 uploads

字符串
1.文件路径构造:请仔细检查$dir_path变量的构造,确保base_app包含您应用的正确基路径,您可以使用echo $dir_path;进行调试,看看路径构造是否正确。
1.数据库连接:在执行SQL查询前,请确认数据库连接($this->conn)成功,如果连接失败,则不执行查询,数据库字段为NULL。
范例:

if ($this->conn->connect_error) {
    die("Connection failed: " . $this->conn->connect_error);
}


1.错误处理:实现正确的错误处理以捕获查询执行期间可能发生的任何SQL错误。这有助于确定SQL语句是否存在问题。
范例:

$qry = $this->conn->query("UPDATE users set avatar = concat('{$fname}','?v=',unix_timestamp(CURRENT_TIMESTAMP)) where id = '$id' ");
if (!$qry) {
    echo "Error: " . $this->conn->error;
}


1.数据库字段类型:确保数据库表中的avatar字段具有合适的数据类型(VARCHAR或TEXT)来存储文件路径。
1.检查SQL注入:虽然提供的代码没有直接在SQL查询中显示用户输入,但始终要小心SQL注入。如果$id变量来自用户输入,请考虑使用预处理语句来防止SQL注入。
范例:

$stmt = $this->conn->prepare("UPDATE users set avatar = concat(?,'?v=',unix_timestamp(CURRENT_TIMESTAMP)) where id = ?");
$stmt->bind_param("si", $fname, $id);
$stmt->execute();
$stmt->close();


通过检查这些点,您可以识别潜在的问题,并解决MySQL数据库中图像路径可能显示为NULL的原因。

相关问题