简体   繁体   中英

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 "";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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