开发者

How to store image file into database in php?

$src = imagecreatefromjpeg($folder.$filename);

$tmp = imagecreatetruecolor($targ_w, $targ_h);
imageco开发者_Go百科pyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);
imagejpeg($tmp, $folder.'t_'.$filename,100);

imagedestroy($tmp);
imagedestroy($src);

how to store $src file into databse


For example, open file using fopen(), read its content using fread(), and then store that content to, e.g. BLOB field in database.

It's always better solution to store file(s) on file system like Dagon suggested in comment.


If at all possible I would avoid storing the image in the database. BLOBS are rarely a good idea and are a total nightmare for portability. I would store the image on the server and then store a reference (either just directory path or URL) to the image in the database. I have a lot of applications that accept file uploads and I always just write the file, apply some sort of naming convention to the file and store the path/url in the database.


You can simply read the file, base 64 encode it and store that as a string. then you may simply call the string contents and output your data as follows <img src="data:image/jpeg;base64,$base64_string_contents">

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜