1+ <?php
2+ header ('Access-Control-Allow-Origin: * ' );
3+ header ('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With ' );
4+ require_once ("../include/dbconfig.php " );
5+
6+ $ postdata = file_get_contents ("php://input " );
7+ if (isset ($ postdata )) {
8+ $ request = json_decode ($ postdata );
9+ $ title = $ request ->title ;
10+ $ detail = $ request ->detail ;
11+ $ image = $ request ->image ;
12+ $ created_at = date ("Y-m-d H:i:s " );
13+
14+ $ database = new Database ();
15+ $ db = $ database ->dbConnection ();
16+ $ con = $ db ;
17+
18+ //upload base64 image
19+ $ target_path = "../uploads/blog/ " ;
20+ $ image_name = uniqid ().".jpg " ;
21+ $ target_path = $ target_path . $ image_name ;
22+
23+ file_put_contents ($ target_path ,base64_decode ($ image ));
24+
25+ try
26+ {
27+
28+ $ stmt = $ con ->prepare ("INSERT INTO clothy_blog(title, detail, image, created_at, updated_at)
29+ VALUES(:name, :detail, :image, :created_at, :updated_at) " );
30+ $ stmt ->bindparam (":name " , $ name );
31+ $ stmt ->bindparam (":detail " , $ detail );
32+ $ stmt ->bindparam (":image " , $ image_name );
33+ $ stmt ->bindparam (":created_at " , $ created_at );
34+ $ stmt ->bindparam (":updated_at " , $ created_at );
35+ $ stmt ->execute ();
36+ echo '{"status": "success"} ' ;
37+ }
38+ catch (PDOException $ e )
39+ {
40+ echo $ e ->getMessage ();
41+ }
42+ } else {
43+ echo '{"status": "error"} ' ;
44+ }
45+
46+ ?>
0 commit comments