Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //importing dbDetails file
- require_once 'connection.php';
- //this is our upload folder
- $upload_path = 'uploads/';
- //Getting the server ip
- $server_ip = gethostbyname(gethostname());
- echo gethostname() . "\n";
- echo gethostbyname(gethostname());
- //creating the upload url
- $upload_url = 'http://'.$server_ip.'/pibg/'.$upload_path;
- //response array
- $response = array();
- if($_SERVER['REQUEST_METHOD']=='POST'){
- //checking the required parameters from the request
- if(isset($_POST['name']) and isset($_FILES['pdf']['name'])){
- //connecting to the database
- //$con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die('Unable to Connect...');
- //getting name from the request
- $name = $_POST['name'];
- //getting file info from the request
- $fileinfo = pathinfo($_FILES['pdf']['name']);
- //getting the file extension
- $extension = $fileinfo['extension'];
- //file url to store in the database
- $file_url = $upload_url . getFileName() . '.' . $extension;
- //file path to upload in the server
- $file_path = $upload_path . getFileName() . '.'. $extension;
- //trying to save the file in the directory
- try{
- //saving the file
- move_uploaded_file($_FILES['pdf']['tmp_name'],$file_path);
- $sql = "INSERT INTO tbl_meeting (url, name, username) VALUES ('$file_url','$name','abc')";
- //adding the path and name to database
- if(mysqli_query($conn,$sql)){
- //filling response array with values
- $response['error'] = false;
- $response['url'] = $file_url;
- $response['name'] = $name;
- }
- //if some error occurred
- }catch(Exception $e){
- $response['error']=true;
- $response['message']=$e->getMessage();
- }
- //closing the connection
- mysqli_close($conn);
- }else{
- $response['error']=true;
- $response['message']='Please choose a file';
- }
- //displaying the response
- echo json_encode($response);
- }
- /*
- We are generating the file name
- so this method will return a file name for the image to be upload
- */
- function getFileName()
- {
- $servername = "localhost"; //replace it with your database server name
- $username = "root"; //replace it with your database username
- $password = ""; //replace it with your database password
- $dbname = "pibg";
- // Create connection
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- // Check connection
- if (!$conn)
- {
- die("Connection failed: " . mysqli_connect_error());
- }
- // $con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die('Unable to Connect...');
- $sql = "SELECT id FROM tbl_meeting";
- $sqlresult = mysqli_query($conn,$sql);
- $result = mysqli_fetch_array($sqlresult);
- mysqli_close($conn);
- if($result['id']==null)
- return 1;
- else
- return ++$result['id'];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement