Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to php and need help.
I'm not able download a file & update the DB's log table at the same time.
For more details please, please, please see the Link.

And, My code as below:
PHP
<?php
$DownDir = "http://localhost/SGA-INTRANET/".$kmsroot. "/". $user. "/". $bu. "/". $client. "/". $project."/Final";
$filenames = "file.xlsx";

$pathDown1 = $DownDir. "/".$filenames;//download path

//using download functionality of HTML 5
echo '<a download="Final_File_'.$filenames.'" name="save" href="'.$pathDown1.'?id='.$name.'&filepath='.$pathDown1.'" target="_blank"><img src="images/Save File.png" title="Download the file"/></a>';
?>

<?php 

$ide = $_GET['id'];//EmployeeID
$file = $_GET['filepath'];// Path

if(isset($ide) && isset($file)) 
{
 if(file_exists($file)) 
  {
    //save log to DB table 'tbl_km_file_download'
    $mysqli = new mysqli("localhost", "USERNAME", "PASSWORD", "DBNAME");
    $mysqli->query("INSERT INTO `tbl_km_file_download` (`name`, `filename`) VALUES ('$ide',  '$file')");
  }
}

?>
Posted
Comments
Kornfeld Eliyahu Peter 1-Sep-14 2:33am    
Debug and see that $ide and $file are never set!!!
GoneBump 1-Sep-14 3:14am    
Hi Kornfeld Eliyahu Peter, thanks for the reply. I checked it but they are not setting as on the click of the download link the document is downloaded but the 'id' and 'filepath' are not setting. Is there a way to set them in the current scenario?
Kornfeld Eliyahu Peter 1-Sep-14 3:16am    
So the question is - why $ide and $file are not set?! That's because you never set them in any way!
GoneBump 1-Sep-14 4:11am    
But I set them @

href="'.$pathDown1.'?id='.$name.'&filepath='.$pathDown1.'"

and

$ide = $_GET['id'];//EmployeeID
$file = $_GET['filepath'];// Path
Kornfeld Eliyahu Peter 1-Sep-14 4:14am    
And why do you think that, that link comes to your page?

1 solution

The Best thing you can do here is redirect it to the second php page when the user clicks and in the second php file set the values in the database and then force the file download from php using readfile.

PHP
$file = <file_path>;

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}</file_path>
 
Share this answer
 
Comments
GoneBump 3-Sep-14 1:15am    
Thank you so much, it's working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900