Here i have a problem with the download.php
. I am trying to download file but its not redirecting to the right path where i shared a folder on the ftp server. Its always redirecting to the address where all my file.php
exist.
Here is my home download.php
<?php
include 'dbconfig';
include 'ftpconfig.php';
include 'aes.php';
session_start();
if($_SESSION['user'] and ($_SESSION['nik'])){
}
else{
header("location:index.php");
}
$file = $_GET['id'];
$fileFtp = 'ftp://'.$ftp_user_name.':'.$ftp_user_pass.'@'.$ftp_server.'/'.$file;
$crypt = new aes_encryption();
$fileName = str_replace('.enc','',$fileFtp.$file);
$fileNamee = str_replace('.enc','',$file);
$dekrip = $crypt->decrypt_file($fileFtp.$file, $fileName);
if (!$dekrip) {
echo "Gagal";
}
else {
echo '<script> window.location.href = '.$fileName,'; </script>';
echo "The file ".$fileNamee." downloaded sucessfully";
}
?>
This is the code on my home.php
to download
<td> <a href="download.php=<?php echo $row['file'] ?>" target="_blank">Download</a></td>
When i click the download, its redirect to where the all file.php
exist
http://localhost/aes/download.php81208-mklh.pdf.enc
not in the shared folder on ftp server where i set in F:\ftp
. and all the files is in that folder.
How can i fix that?
You're missing ?id
in your link before the =
sign:
<a href="download.php?id=<?php echo $row['file'] ?>"...
Plus, seeing http://localhost/aes/download.php
change it to:
<a href="/aes/download.php?id=<?php echo $row['file'] ?>"...
or
<a href="http://localhost/aes/download.php?id=<?php echo $row['file'] ?>"...
depending on the file's (download.php) location on your server.
Error reporting should be set to catch and display notices.
Make sure the path is correct and that your database query is correct with no errors, because $row['file']
suggests it.
N.B.: (edit)
Both your FTP and DB code need to be inside the same file. You're probably losing a connection somewhere by having your code in separate files.
References:
and use var_dump();
and print_r();
and your HTML source as debugging tools.
Also make sure that your database's query in a while
loop, doesn't end prematurely. Seeing another question of yours, you are using mysql_
.
I.e.: (notice the semi-colon at the end)
while ($row = mysql_fetch_array($result)); // <<< that terminates it.
{
$file = $row["file"];
}
I have seen this happen before, it's an insight.
If you put a header before any HTML that is dumped to the client you will redirect using PHP (server side, so you can send headers):
header('Location: http://www.yoursite.com/file_location.php');
If you want to do a JavaScript redirect you will use something like (client side, so the page can start loading already):
<script type="text/javascript">
<!--
window.location = "http://www.yoursite.com/file_location.php"
//-->
</script>