0

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 "";
}
Akisha
  • 113
  • 1
  • 1
  • 10
  • http://stackoverflow.com/questions/17717506/how-to-upload-images-into-mysql-database-using-php-code – Gustavo Henrique Jul 01 '15 at 17:07
  • Thank u for your referance. I actually need something different from this as I'm adding much more information along with this image as well. So adding into a db like above would not be the correct way! – Akisha Jul 01 '15 at 17:16

1 Answers1

0

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 "";
}
Charly
  • 101
  • 1
  • 8