File: /home/barbeatleanalyti/www/manage.beatleanalytics.com/upload_image.php
<?php
/*
* Following code will get single department details
* A industry is identified by dept_id
*/
// array for JSON response
$response = array();
// include db connect class
require_once 'db_connect_api.php';
// connecting to db
$db = new DB_CONNECT();
//Getting the server ip
$server_ip = gethostbyname(gethostname());
//this is our upload folder
$upload_path = 'documents/local/webprofileimages/';
//creating the upload url
$upload_url = 'http://localhost/owner.beatleanalytics.com/documents/local/webprofileimages/';
// echo $_SERVER['DOCUMENT_ROOT'] ;
if($_SERVER['REQUEST_METHOD']=='POST'){
//checking the required parameters from the request
if(isset($_POST['name']) and isset($_FILES['image']['name']) and isset($_POST['id'])){
//getting name from the request
$name = $_POST['name'];
$id = $_POST['id'];
//getting file info from the request
$fileinfo = pathinfo($_FILES['image']['name']);
//getting the file extension
$extension = $fileinfo['extension'];
$uFileName = getFileName();
//file url to store in the database
$file_url = $upload_url . $uFileName . '.' . $extension;
//file path to upload in the server
$file_path = $upload_path . $uFileName . '.'. $extension;
//trying to save the file in the directory
$file_name = $uFileName . '.'. $extension;
try{
//saving the file
move_uploaded_file($_FILES['image']['tmp_name'],$file_path);
$sql = "UPDATE beatle_userlogin SET webprofileimage = '$file_name' where userId = '$id' ;";
//adding the path and name to database
if(mysql_query($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();
}
//displaying the response
echo json_encode($response);
}else{
$response['error']=true;
$response['message']='Please choose a file';
}
}
/*
We are generating the file name
so this method will return a file name for the image to be upload
*/
function getFileName(){
$len = 22;
$rand = '';
while( !( isset( $rand[$len-1] ) ) ) {
$rand .= mt_rand( );
}
return "webimage_".substr( $rand , 0 , $len );
}
?>