Advertisement
Guest User

Untitled

a guest
Jan 1st, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.05 KB | None | 0 0
  1. <?php
  2.  
  3. //importing dbDetails file
  4. require_once 'connection.php';
  5.  
  6. //this is our upload folder
  7. $upload_path = 'uploads/';
  8.  
  9. //Getting the server ip
  10. $server_ip = gethostbyname(gethostname());
  11.  
  12. echo gethostname() . "\n";
  13. echo gethostbyname(gethostname());
  14.  
  15. //creating the upload url
  16. $upload_url = 'http://'.$server_ip.'/pibg/'.$upload_path;
  17.  
  18. //response array
  19. $response = array();
  20.  
  21.  
  22. if($_SERVER['REQUEST_METHOD']=='POST'){
  23.  
  24.     //checking the required parameters from the request
  25.     if(isset($_POST['name']) and isset($_FILES['pdf']['name'])){
  26.  
  27.         //connecting to the database
  28.         //$con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die('Unable to Connect...');
  29.  
  30.         //getting name from the request
  31.         $name = $_POST['name'];
  32.  
  33.         //getting file info from the request
  34.         $fileinfo = pathinfo($_FILES['pdf']['name']);
  35.  
  36.         //getting the file extension
  37.         $extension = $fileinfo['extension'];
  38.  
  39.         //file url to store in the database
  40.         $file_url = $upload_url . getFileName() . '.' . $extension;
  41.  
  42.         //file path to upload in the server
  43.         $file_path = $upload_path . getFileName() . '.'. $extension;
  44.  
  45.         //trying to save the file in the directory
  46.         try{
  47.             //saving the file
  48.             move_uploaded_file($_FILES['pdf']['tmp_name'],$file_path);
  49.             $sql = "INSERT INTO tbl_meeting (url, name, username) VALUES ('$file_url','$name','abc')";
  50.            
  51.             //adding the path and name to database
  52.             if(mysqli_query($conn,$sql)){
  53.                 //filling response array with values
  54.                 $response['error'] = false;
  55.                 $response['url'] = $file_url;
  56.                 $response['name'] = $name;
  57.             }
  58.             //if some error occurred
  59.         }catch(Exception $e){
  60.             $response['error']=true;
  61.             $response['message']=$e->getMessage();
  62.         }
  63.         //closing the connection
  64.         mysqli_close($conn);
  65.     }else{
  66.         $response['error']=true;
  67.         $response['message']='Please choose a file';
  68.     }
  69.    
  70.     //displaying the response
  71.     echo json_encode($response);
  72. }
  73.  
  74. /*
  75. We are generating the file name
  76. so this method will return a file name for the image to be upload
  77. */
  78.  
  79. function getFileName()
  80. {          
  81.     $servername = "localhost"; //replace it with your database server name
  82.     $username = "root";  //replace it with your database username
  83.     $password = "";  //replace it with your database password
  84.     $dbname = "pibg";
  85.     // Create connection
  86.     $conn = mysqli_connect($servername, $username, $password, $dbname);
  87.     // Check connection
  88.     if (!$conn)
  89.     {
  90.         die("Connection failed: " . mysqli_connect_error());
  91.     }
  92.    
  93.     // $con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die('Unable to Connect...');
  94.     $sql = "SELECT id FROM tbl_meeting";
  95.     $sqlresult = mysqli_query($conn,$sql); 
  96.     $result = mysqli_fetch_array($sqlresult);
  97.  
  98.     mysqli_close($conn);
  99.     if($result['id']==null)
  100.         return 1;
  101.     else
  102.         return ++$result['id'];
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement