I have created a php file which work is to save the file in the server folder and save the url of that file in the database but their is some issue the url should be saved but files does not move the server folder. I want to do that if user upload a file then that file should be saved in a particular folder which created in public HTML and save that file url to the database. But in my case only url is saved to the database but file does not moved in particular folder. I don't known by this happen. my upload code is `
define('DB_HOST','localhost');
define('DB_USERNAME','username');
define('DB_PASSWORD','password');
define('DB_NAME','dbname');
//this is our upload folder
$upload_path = 'pdffiles/';
//creating the upload url
$upload_url = 'https://balajismajkalyansantha.000webhostapp.com/'.$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 `id4834528_try`.`pdf` (`id`, `url`, `name`) VALUES (NULL, '$file_url', '$name');";
//adding the path and name to database
if(mysqli_query($con,$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($con);
}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(){
$con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die('Unable to Connect...');
$sql = "SELECT max(id) as id FROM pdf";
$result = mysqli_fetch_array(mysqli_query($con,$sql));
mysqli_close($con);
if($result['id']==null)
return 1;
else
return ++$result['id'];
}`
can any one help me.
Make sure the folder 'pdffiles' has public write access to it. (0777)