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

时间:2015-07-01 17:05:47

标签: php mysql phpmyadmin

我需要将图像上传到数据库并将其保存在文件夹中,这样我就可以通过提供该文件夹的路径来显示图像。我已经对此进行了编码,但在保存文件夹内存在问题。它保存了数据库的路径并正确存储了详细信息,但它不会保存在我在项目中创建的文件夹中。文件夹名称也相同但我无法解决此问题。请帮我找到纠正这个。我一直在努力解决这个问题很长一段时间。

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

1 个答案:

答案 0 :(得分:0)

尝试通过绝对路径更改相对路径。

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