php - Unable to post image and text to database. Receiving no errors -
hey guys having issues php file supposed allow user post status along picture uploaded server , path along username of user added db.
db colomns:
postid (a.i)
username
status
imagepostpath
timestamp (added automatically inserting new entry)
extra info: have changed code 1 of working ones, when attempt test php file postman error "[]".
i'm not familiar php if see mistake i'm making simple, please me understand :) here code:
<?php //importing dbdetails file require_once 'dbdetails.php'; //this our upload folder $upload_path = '000002/'; //getting server ip $server_ip = gethostbyname(gethostname()); //creating upload url $upload_url = 'http://'.$server_ip.'/users/images/'.$upload_path; //response array $response = array(); if($_server['request_method']=='post'){ //checking required parameters request if(isset($_post['name']) , isset($_files['image']['name'])){ //connecting database $con = mysqli_connect(host,user,pass,db) or die('unable connect...'); //getting name request $name = $_post['name']; $status = $_post['status']; $timestamp = date('y-m-d h:i:s'); //getting file info request $fileinfo = pathinfo($_files['image']['name']); //getting file extension $extension = $fileinfo['extension']; //file url store in database $file_url = $upload_url . getfilename() . '.' . $extension; //file path upload in server $file_path = $upload_path . getfilename() . '.'. $extension; //trying save file in directory try{ //saving file move_uploaded_file($_files['image']['tmp_name'],$file_path); $sql = "insert `flare`.`tbl_user_feed` (`postid`, `username`, `status`, `imagepostpath`, `timestamp`) values (null, '$name', '$status', '$file_url');"; //adding path , name database if(mysqli_query($con,$sql)){ //filling response array values $response['error'] = false; $response['name'] = $name; $response['imagepostpath'] = $file_url; } //if error occurred }catch(exception $e){ $response['error']=true; $response['message']=$e->getmessage(); } //displaying response echo json_encode($response); //closing connection mysqli_close($con); }else{ $response['error']=true; $response['message']='please choose file'; } } /* generating file name method return file name image upload */ function getfilename(){ $con = mysqli_connect(host,user,pass,db) or die('unable connect...'); $sql = "select max(postid) postid tbl_user_feed"; $result = mysqli_fetch_array(mysqli_query($con,$sql)); mysqli_close($con); if($result['postid']==null) return 1; else return ++$result['postid']; } ?>
change these lines:
move_uploaded_file($_files['image']['tmp_name'],$file_path);
your file path same old files being overwritten new...randomize md5()
$unix = time(); $file_path = $upload_path . getfilename() . md5($unix) . '.'. $extension;
then alter query slightly
$sql = "insert `flare`.`tbl_user_feed` (`postid`, `username`, `status`, `imagepostpath`, `timestamp`) values (null, '$name', '$status', '$file_url', '$unix')";// remove semicolon before last double quote , add value 5th column
Comments
Post a Comment