Hello I'm doing my project and i want to ask just a simple question. Every time I go to that page it downloads automatically without getting inside on that page. When I click the button it automatically download the files. But I want to go that page and click a link before downloading it. Anyone help?
if(isset($_GET['profile']))
{
$id=$_GET['profile'];
$query = "SELECT * FROM profile_table WHERE id=?";
$stmt=$con->prepare($query);
$stmt->bind_param("i",$id);
$rc = $stmt->execute();
if ( false===$rc ) {
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$vid = $row['id'];
$vfname = $row['fname'];
$vlname = $row['lname'];
$vmname = $row['mname'];
$vsex = $row['sex'];
$vage = $row['age'];
$vbday = $row['bday'];
$vbloodtype = $row['bloodtype'];
$vheight = $row['height'];
$vweight = $row['weight'];
$vreligion = $row['religion'];
$vcolorhair = $row['colorhair'];
$vdistmark = $row['distmark'];
$vmobile = $row['mobile'];
$vtin = $row['tin'];
$vphealth = $row['phealth'];
$vlegaldep = $row['legaldep'];
$vaddress = $row['address'];
$vtsize = $row['tsize'];
$vheadgear = $row['headgear'];
$vshoes = $row['shoes'];
$vuniform = $row['uniform'];
$vphoto = $row['photo'];
$vsoi = $row['soi'];
if (file_exists($vsoi)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($vsoi));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('documents/' . $row['name']));
readfile('documents/' . $row['name']);
}
}
I think the problem their is the file_exist in vsoi. And I dont know what to do or to change in my code. I appreciate all the answers. TIA!
I think first you need to understand what your code is trying to do, e-g
if (file_exists($vsoi)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($vsoi));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('documents/' . $row['name']));
readfile('documents/' . $row['name']);
}
Above code is used to when we want to force a browser to download a file.
But in your case you only want to download the file when the page is loaded and user clicks on a download link and then the download starts.
It can be achieved by just displaying a download link which will then execute the same code you wrote in the if statement.
So change your if statement as following and then write the same code which was in the if statement previously to that download link page.
if (file_exists($vsoi)) {
echo 'Download File
}
You can simply display a direct download link to the file as following.
<php
$download_link = 'https://yourdomain.com/files/sample.pdf';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>Sample file</h1>
Download
</body>
</html>
Related
I am using this code to view an PDF file in php instead of just opening it in an browser.
if(isset($_SESSION['access_token']))
{
require_once('includes/db_connect.php');
$id = mysqli_real_escape_string($mysqli, $_GET['id']);
$attest = mysqli_real_escape_string($mysqli, $_GET['attest']);
$heat = mysqli_real_escape_string($mysqli, $_GET['heat']);
$mat = mysqli_real_escape_string($mysqli, $_GET['mat']);
$dikte = mysqli_real_escape_string($mysqli, $_GET['dikte']);
$file = '../thure/uploads/attesten/'.$id.'.pdf';
$filename = 'Certificaat plaat '.$id.' met '.$attest.' heat '.$heat.' '.$mat.' '.$dikte.'mm.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
}
The title of the page as Shown in Explorer, Chrome etc. is the name of the php file. How can I set an custom name (same as PDF) here?
When I use the code below the pdf is shown as plain text.
<!DOCTYPE html>
<head>
<title>Torza</title>
</head>
I am new to PHP and trying to implement below functionality through code give
Functionality:
I have one Download button and upon clicking i should be able to download my .pdf file stored under htdocs-->xxfilename-->abc.pdf;
Code:
I am using below code in my xyz.php web page
<?php
$title = "Learning";
$content = '
<h3> Intro</h3>
<p>
Introduction goes here
</p>
<form action = "xyz.php" method = "post" name = "downloadform">
<input type="submit" value="Download" name="dwnld_file"/>
</form>
';
if (isset($_POST['dwnld_file'])) {
mysql_connect("localhost", "root", "");
mysql_select_db("PQR");
$res = mysql_query("Select * from tab1");
while ($row = mysql_fetch_array($res)) {
$file = '$row["col2"]';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $row["col2"] . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Length:' . filesize($file));
readfile($file);
}
}
include 'Template.php';
?>
Error:
My pdf file is getting downloaded but upon opening it it says "failed to load pdf document"
Please help where i am wrong.
***Edit****
I tried different approach and my file is downloading but still it says "failed to load pdf document"
My other approach code is below
<form action = "xyz.php" method = "post" name = "downloadform">
<input type="submit" value="Download " name="dwnld_file"/>
</form>
<?php
if (isset($_POST['dwnld_file'])) {
mysql_connect("localhost", "root", "");
mysql_select_db("test");
$res = mysql_query("Select * from tab1");
while ($row = mysql_fetch_array($res)) {
$file = $row["col1"];
echo $file;
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' .$file. '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Length:' . filesize($file));
readfile('myfile/'.$file);
}
}
?>
Please tell me if i am doing anything wrong.
I implemented another solution after doing some more study and i was able to solve the problem.
Below is my code i used to implement.
Concept:
1) We can use HTML+JSP+PHP to get this functionality.
2) At HTML + JSP side we need to call eventListener to capture the event of clicking the button.
3) This event will redirect to the php page which will download the pdf file to your local computer.
Code Snippet:(here XYZ is the name of page where you want to show Download button)
in XYZ.php
<input type="button" id="btnshow" value="Download" name="dwnld" />
<script>
var btn = document.getElementById('btnshow');
btn.addEventListener('click', function () {
document.location.href = '<?php echo 'download.php'; ?>'
})
</script>
in download.php
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("database_name");
$res = mysql_query("Select * from table_name");
while ($row = mysql_fetch_array($res)) {
$file = 'Pdf_File/' . $row["Path"];
$filename = $row["Path"];
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
readfile($file);
}
?>
Hope it help some one out there newbie like me.
Happy Coding!
Recently moved a site over to a new server and now for some reason the download page is no longer downloading the purchased files.
When the download page is reached it is simply blank. I have changed the filename directory to use home/mastetu8 instead of home/mastvoic which was the last directory. Any idea on why the page is blank when trying to download?
Here is the download_process.php file currently running this piece:
<?php ob_start(); ?>
<?php require_once('../Connections/admin.php'); ?>
<?php
$orders_id = $_GET['orderid'];
$id = $_GET['id'];
mysql_select_db($database_admin, $admin);
$query_rsOrdersDetail = "SELECT * FROM orders_detail LEFT JOIN media ON orders_detail.product_id = media.sortorder WHERE orders_detail.orders_id = '$orders_id' AND orders_detail.id = '$id'";
$rsOrdersDetail = mysql_query($query_rsOrdersDetail, $admin) or die(mysql_error());
$row_rsOrdersDetail = mysql_fetch_assoc($rsOrdersDetail);
$totalRows_rsOrdersDetail = mysql_num_rows($rsOrdersDetail);
// CHECK DOWNLOAD COUNT
if ($row_rsOrdersDetail['downloadcount'] <= 3) {
$filename_hires = '/home/mastetu8/public_html'.$row_rsOrdersDetail['filename_hires'];
if (file_exists($filename_hires)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename_hires));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename_hires));
ob_clean();
flush();
readfile($filename_hires);
// UPDATE DOWNLOAD COUNTER
$downloadcount_new = $row_rsOrdersDetail['downloadcount'] + 1;
mysql_select_db($database_admin, $admin);
mysql_query("UPDATE orders_detail SET downloadcount='$downloadcount_new' WHERE id = '$id' AND orders_id = '$orders_id'", $admin) or die(mysql_error());
}
}
?>
When using computer files are downloaded normally.
Problem start on android browsers.
When we download a file from android default browser, the file is downloaded but it is of 0.00 bytes, so not technically present there(corrupt file).
When we download file from any third party application like or opera it gives error that "file could not be downloaded." not the header error. And files could be downloaded via UC Browser and Chrome and Firefox. But still gives the error in default browser.
Here is the code I am using:
<?php
require 'php/db.php';
if(isset($_POST['file_id'])&&!empty($_POST['file_id'])){
download_file($_POST['file_id']);
}
function download_file($id){
global $con;
$id = mysqli_real_escape_string($con,htmlentities($id));
$file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id";
$result = mysqli_query($con,$file);
$row = mysqli_fetch_assoc($result);
$name = $row['file_name'];
$title = $row['file_title'];
$size = $row['file_size'];
$ext = strtoupper(ext($name)); // Function defined bellow
$down = $row['down'];
$newname = $title.'.'.$ext;
$olddir = "files/".$name;
$down++;
if(is_file($olddir)) {
$update_down = "UPDATE files SET down = $down WHERE file_id = '$id'";
$update_down_result = mysqli_query($con,$update_down);
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($olddir)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$newname.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$size); // provide file size
header('Connection: close');
readfile($olddir);
exit();
}else header("Location: index.php?msg=Sorry!+File+could+not+be+downloaded");
}
function ext($name){
$rut = strrev($name);
$erut = explode('.', $rut);
return strrev($erut[0]);
}
We give file id to this function and it downloads the file on PC.
Can anyone tell me how to remove the error? So that users can download files from their android phones too.
Probably $size == 0;
$size = $row['file_size'];
...
$olddir = "files/".$name;
change to
$olddir = "files/".$name;
$size = filesize($olddir);
And change
else header("Location: index.php?msg=Sorry!+File+could+not+be+downloaded");
to
else header("Location: index.php?msg=Sorry!+File+could+not+be+found+on+server: " . $olddir);
I got the solution.
Mainly I changed two things:
Used GET method instead of Post Method.
Used Flush and ob_clean functions to clear the buffer.
New code for downfile.php is like this:
<?php
require '../php/db.php';
ob_start();
if(isset($_GET['file_id'])&&!empty($_GET['file_id'])){
download_file($_GET['file_id']);
}else die("There was an error in downloading file. Please try again later.");
function download_file($id){
global $con;
$id = mysqli_real_escape_string($con,htmlentities($id));
$file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id";
$result = mysqli_query($con,$file);
$row = mysqli_fetch_assoc($result);
$name = $row['file_name'];
$title = $row['file_title'];
$ext = ext($name);
$down = $row['down'];
$newname = $title.'.'.$ext;
$size = $row['file_size'];
$down++;
if(is_file($name)) {
$update_down = "UPDATE files SET down = $down WHERE file_id = '$id'";
$update_down_result = mysqli_query($con,$update_down);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$newname.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.$size);
ob_clean();
flush();
readfile($name);
exit;
}else header("Location: index.php?msg=Sorry!+File+could+not+found!");
}
function ext($name){
$rut = strrev($name);
$erut = explode('.', $rut);
return strrev($erut[0]);
}
?>
Through this code I am able to download files in android browsers too.
Hope this may help. :)
I'm trying to make an interface where user can log in and download files from a ftp server. I got a problem with my download links, and it only downloads the last file in the list even if i press the first.
Here is my code:
$a = ftp_nlist($conn_id, "/web/images");
foreach($a as $value){
if ($i <= 1) {
$i ++;
}
else {
echo "<br>";
echo '<ul><li>'."$value".'</li>';
echo '<li><a href="download.php?="' . $value . '>Download</a></li>';
echo '<li>Preview</li></ul>';
$_SESSION['download'] = $value;
}
}
?>
And here is download.php:
<?php
session_start();
$value = $_SESSION['download'];
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($value));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($value));
?>
As you can see I'm fairly new to php so any help would be great!
Thanks.
The value of $_SESSION['download']; is always the last file on the FTP connection.
Since you are throwing the path via GET, you should use it instead.
Change
$value = $_SESSION['download'];
To
$value = $_GET['download'];
try this :-
echo '<li><a href="download.php?="' . $value . '>Download</a></li>';
change this line to this
echo '<li><a href="download.php?download="' . $value . '>Download</a></li>';
and in the download.php get the value of download like
$value = $_GET['download'];