What's new

Anu kaay problima sa code ko

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,168
Solutions
2
Reaction
103
Points
497
Age
30
di madownload yun galing sa database ko na pdf.. kapag pinidindot ko

ito code ko sa upload.php
<?php
include '../config/config.php';
?>
<?php
if(isset($_POST['submit'])){
$targetDir = "upload/";
$targetfile = $targetDir . basename($_FILES["pdfFile"]["name"]);
$filetype =strtolower(pathinfo($targetfile,PATHINFO_EXTENSION));
// check if file is a PDF and less than 20mb
if ($filetype == "pdf" && $_FILES['pdfFile']["size"] <= 5000000) {
if (move_uploaded_file($_FILES["pdfFile"]["tmp_name"], $targetfile)) {
echo "PDF file uploaded successfully.";
} else {
echo "Error uploading PDF file.";
}
} elseif ($filetype == "docx" && $_FILES["pdfFile"]["size"] <= 100000000) {
if (move_uploaded_file($_FILES["pdfFile"]["tmp_name"], $targetfile)) {
echo "Word file uploaded successfully.";
} else {
echo "Error uploading Word file.";
}
} else {
echo "Error: Invalid file type or size.";
}
$filename = $_FILES["pdfFile"]["name"];
$folder_path = $targetDir;
$time_stamp = date('Y-m-d H:i:s');
$stmt= $con ->prepare("INSERT INTO upload (filename,file_path, datetime) VALUES (?,?,?)");
$stmt->bind_param ("sss", $filename,$folder_path, $time_stamp);

if ($stmt->execute()){
echo "File Uploaded Successfully,";
}else{
echo "error";
}
$stmt->close();
$con->close();
}
?>

ito code ko naman sa form.php
<table class="tableload">
<tr class="trload">
<td class="tdload">
<ul>
<li><a href="download.php?id=<?php echo $fileupload; ?>">Download</a></li>
</ul>
</td>

</tr'>
</table>
<?php


ito code ko download.php

<?php
include '../config/config.php';
if (isset($_GET['id'])) {
$fileupload = $_GET['id'];
$stmt = $con->prepare("SELECT filename, file_path FROM upload WHERE id = ?");
$stmt->bind_param("i", $fileupload);
$stmt->execute();
$stmt->bind_result($filename, $file_path);
$stmt->fetch();
$stmt->close();
$fullFilePath = $file_path . $filename;
echo ($fullFilePath);
if (file_exists($fullFilePath)) {
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
readfile($fullFilePath);
} else {
echo "File not found.";
}
}
// header('Location:form.php');
?>
 
Based on the code you provided, there are a few potential issues that could prevent the PDF file from being downloaded.

1. In your form.php file, the line <li><a href="download.php?id=<?php echo $fileupload; ?>">Download</a></li> implies that you are passing the $fileupload variable as the ID parameter to download.php. However, in your download.php file, you are using $_GET['id'] to retrieve the ID. Make sure that the $fileupload variable is properly assigned in form.php.

2. In your download.php file, you are concatenating the file path and filename to create $fullFilePath. However, it seems that the $file_path variable from the database does not include a trailing slash ("/") at the end. To fix this, you can add a trailing slash to the $file_path variable in the SQL query or manually add it to the $fullFilePath variable.

3. Ensure that the file path and filename stored in the database are correct and match the actual file location on the server. You can verify this by echoing the $fullFilePath variable before the if (file_exists($fullFilePath)) condition in download.php.

4. Make sure that the file you are trying to download is actually located in the specified directory (upload/) on the server.

5. Check the file permissions of the upload directory and the files within it. The web server needs to have sufficient permissions to access and read the files.

By addressing these potential issues, you should be able to download the PDF file from your database successfully.
 
for Upload.php

<?php
include '../config/config.php';

if(isset($_POST['submit'])){
$targetDir = "upload/";
$targetfile = $targetDir . basename($_FILES["pdfFile"]["name"]);
$filetype = strtolower(pathinfo($targetfile, PATHINFO_EXTENSION));

// Allowed file types and their corresponding size limits
$allowedTypes = array("pdf" => 5000000, "docx" => 100000000);

// Check if the file type is allowed and size is within limits
if (array_key_exists($filetype, $allowedTypes) && $_FILES['pdfFile']['size'] <= $allowedTypes[$filetype]) {
if (move_uploaded_file($_FILES["pdfFile"]["tmp_name"], $targetfile)) {
echo "File uploaded successfully.";
} else {
echo "Error uploading file.";
}

$filename = $_FILES["pdfFile"]["name"];
$folder_path = $targetDir;
$time_stamp = date('Y-m-d H:i:s');

$stmt = $con->prepare("INSERT INTO upload (filename, file_path, datetime) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $filename, $folder_path, $time_stamp);

if ($stmt->execute()){
echo "File Uploaded Successfully.";
} else {
echo "Error uploading file information.";
}
$stmt->close();
} else {
echo "Error: Invalid file type or size.";
}
$con->close();
}
?>

for form.php

<table class="tableload">
<tr class="trload">
<td class="tdload">
<ul>
<li><a href="download.php?id=<?php echo $fileupload; ?>">Download</a></li>
</ul>
</td>
</tr>
</table>

for download.php

<?php
include '../config/config.php';

if (isset($_GET['id'])) {
$fileupload = $_GET['id'];

$stmt = $con->prepare("SELECT filename, file_path FROM upload WHERE id = ?");
$stmt->bind_param("i", $fileupload);
$stmt->execute();
$stmt->bind_result($filename, $file_path);
$stmt->fetch();
$stmt->close();

$fullFilePath = $file_path . $filename;

if (file_exists($fullFilePath)) {
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
header("Content-Length: " . filesize($fullFilePath));

// Read and output the file content
readfile($fullFilePath);
} else {
echo "File not found.";
}
} else {
echo "Invalid file ID.";
}
?>
 

Similar threads

Back
Top