1

i want to upload image in database in php with title name ... like this :: (domain.com/image-ID-title.jpg) -- (domain.com/shd64ht-this-is-my-first-post.jpg)

this is my code and the output is (domain.com/image-ID.jpg) -- (domain.com/shhk87y.jpg)

function store_file($file){
    $ext = explode('.', $file['name']);     
    $ext = $ext[count($ext)-1];

    do {
        $file_name = genfilename($ext);
    } while(file_exists("../img/{$file_name}"));

    move_uploaded_file($file['tmp_name'], "../img/{$file_name}");
    return $file_name;
}

function genfilename($ext){
    $a = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
    $nm = "";
    for($i=1; $i<=8; $i++){
        $nm = $nm . $a[rand(0,35)];
    }
    return $nm . ".{$ext}";
}

if (isset($_POST['save'])) {
    $title_save = $_POST['title'];
    $pic = $_FILES['pic'];

    $pic_name = store_file($pic);

    mysql_query("insert my_posts SET title ='$title_save' ,pic ='$pic_name'") or die(mysql_error());
    $flg_okay = 0;
    $flg_okay = 1;
}
4
  • $_FILES['pic']['name']; Commented Mar 2, 2018 at 9:56
  • please please do not use mysql_* functions. its 2018. Also this code isn't in production yet right? Commented Mar 2, 2018 at 9:57
  • Why are you using the long-deprecated mysql_ code library? It was discontinued many years ago and removed entirely in PHP7. No new code should be written using this library. It leaves you vulnerable to SQL injection attacks (due to the lack of parameterised query support) and potentially other unpatched vulnerabilities. Switch to using mysqli or PDO as soon as possible, and then learn how to write parameterised queries to protect your data from malicious input. See bobby-tables.com for a simple explanation of the risks and some sample PHP code to write queries safely. Commented Mar 2, 2018 at 11:58
  • And then find a tutorial showing you how to deal with file uploads, there are many. It will explain how to access the right parts of the $_FILES array to get what you need Commented Mar 2, 2018 at 11:59

1 Answer 1

1

Not sure if I understand fully what you intend, but I wouldn't advise storing images in the database. What I think you should do is receive an image and store it's information on a table. Something among the lines of:

(using HTTPPost or Put to submit the file, according to whatever validations you'd want)

| id | name           | alias     | path                           | created_at | updated_at |
+----+----------------+-----------+--------------------------------+------------+------------+
|  1 | filestored.jpg | mypicture | path/to/image/withoutimagename | timestamp  | timestamp  |

and retrieving it by using an ID (whatever you feel its appropriate) returning path+name. This path+name can be a url or you can just tell it download a certain file based on http://php.net/manual/en/function.readfile.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.