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;
}
$_FILES['pic']['name'];mysql_*functions. its 2018. Also this code isn't in production yet right?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 usingmysqliorPDOas 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.