简体   繁体   English

如何在php中将图像上传到数据库

[英]How to upload an image to database in php

I need to upload an image to database and save it inside a folder so I can display the image by giving the path to that folder. 我需要将图像上传到数据库并将其保存在文件夹中,这样我才能通过提供该文件夹的路径来显示图像。 I have already coded this but there is a problem it in saving inside a folder. 我已经对此进行了编码,但是将其保存在文件夹中存在问题。 It saves the path to the database and correctly storing details but it dosen't save inside the folder which I have created inside my project.Folder name is also same but I cannot fix this problem. 它保存了数据库的路径并正确地存储了详细信息,但没有保存在我在项目内部创建的文件夹中。文件夹名称也相同,但无法解决此问题。 Please help me find to correct this. 请帮助我找到纠正的方法。 I've been struggling with this for a long time. 我已经为此苦苦挣扎很长时间了。

public function save()
{
    $url = $this->do_upload();
    //$title = $_POST["title"];
    //$this->main_m->save($title, $url);
}
private function do_upload()
{
    $type = explode('.', $_FILES["pic"]["name"]);
    $type = strtolower($type[count($type)-1]);
    $url = "./images/".uniqid(rand()).'.'.$type;
    if(in_array($type, array("jpg", "jpeg", "gif", "png")))
        if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
            if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
                return $url;
    return "";
}

Try change the relative path by a absolute path. 尝试通过绝对路径更改相对路径。

public function save()
{
    $url = $this->do_upload();
    //$title = $_POST["title"];
    //$this->main_m->save($title, $url);
}
private function do_upload()
{
    $type = explode('.', $_FILES["pic"]["name"]);
    $type = strtolower($type[count($type)-1]);
    $url = "/data/images/".uniqid(rand()).'.'.$type;
    if(in_array($type, array("jpg", "jpeg", "gif", "png")))
        if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
            if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
                return $url;
    return "";
}

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

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