Hi ppl,
When i am downloading a file from my uploads folder it always downloads a file named download.htm even though in the folder there is a txt file.
Anyone knows whats the problem?
My upload.php - <?php
-
include_once 'dbconfig.php';
-
if(isset($_POST['btn-upload']))
-
{
-
-
$file = rand(1000,100000)."-".$_FILES['file']['name'];
-
$file_loc = $_FILES['file']['tmp_name'];
-
$file_size = $_FILES['file']['size'];
-
$file_type = $_FILES['file']['type'];
-
$folder="uploads/";
-
-
// new file size in KB
-
$new_size = $file_size/1024;
-
// new file size in KB
-
-
// make file name in lower case
-
$new_file_name = strtolower($file);
-
// make file name in lower case
-
-
$final_file=str_replace(' ','-',$new_file_name);
-
-
if(move_uploaded_file($file_loc,$folder.$final_file))
-
{
-
$sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
-
mysql_query($sql);
-
?>
-
<script>
-
alert('successfully uploaded');
-
window.location.href='view.php?success';
-
</script>
-
<?php
-
}
-
else
-
{
-
?>
-
<script>
-
alert('error while uploading file');
-
window.location.href='upload.php?fail';
-
</script>
-
<?php
-
}
-
}
-
?>
My Download.php - <?php
-
require 'dbconfig.php';
-
-
$query = "SELECT * FROM tbl_uploads";
-
$result = mysql_query($query) or die('Error, query failed');
-
-
if(mysql_num_rows($result)==0){
-
echo "Database is empty <br>";
-
}
-
else{
-
while(list($id, $name) = mysql_fetch_array($result)){
-
echo "<a href=\"download.php?id=\$id\">$name</a><br>";
-
}
-
}
-
-
if(isset($_GET['id'])){
-
$id = $_GET['id'];
-
$query = "SELECT file, type, size FROM tbl_uploads WHERE id = '$id'";
-
$result = mysql_query($query) or die('Error, query failed');
-
list($name, $type, $size, $content) = mysql_fetch_row($result);
-
header("Content-Disposition: attachment; filename=$name");
-
header("Content-type: $type");
-
header("Content-length: $size");
-
print $content;
-
}
-
?>
My view.php (where the download link is) - <?php
-
include_once 'dbconfig.php';
-
if(isset($_GET['remove_id']))
-
{
-
$res=mysql_query("SELECT file FROM tbl_uploads WHERE id=".$_GET['remove_id']);
-
$row=mysql_fetch_array($res);
-
mysql_query("DELETE FROM tbl_uploads WHERE id=".$_GET['remove_id']);
-
unlink("uploads/".$row['file']);
-
header("Location: view.php");
-
}
-
?>
-
<!DOCTYPE html>
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Delete Uploaded Files From Folder in PHP</title>
-
<link rel="stylesheet" href="style.css" type="text/css" />
-
<script type="text/javascript">
-
function remove(id)
-
{
-
if(confirm(' Sure to remove file ? '))
-
{
-
window.location='delete.php?remove_id='+id;
-
}
-
}
-
</script>
-
</head>
-
<body>
-
<div id="header">
-
<label>Delete Uploaded Files From Folder in PHP</label>
-
</div>
-
<div id="body">
-
<table width="80%" border="1">
-
<tr>
-
<th colspan="4">your uploads...<label><a href="relatorios.php">upload new files...</a></label></th>
-
</tr>
-
<tr>
-
<td>File Name</td>
-
<td>File Type</td>
-
<td>File Size(KB)</td>
-
<td>View</td>
-
<td>Delete</td>
-
<td>Download</td>
-
<?php
-
$sql="SELECT * FROM tbl_uploads";
-
$result_set=mysql_query($sql);
-
while($row=mysql_fetch_array($result_set))
-
{
-
?>
-
</tr>
-
-
<tr>
-
<td><?php echo $row['file'] ?></td>
-
<td><?php echo $row['type'] ?></td>
-
<td><?php echo $row['size'] ?></td>
-
<td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">view file</a></td>
-
<td><a href="javascript:remove(<?php echo $row['id'] ?>)">Delete file</a></td>
-
<td><a href="download.php">Download</a></td>
-
</tr>
-
<?php
-
-
}
-
?>
-
</table>
-
-
</div>
-
</body>
-
</html>
Thaks in advance.
Peace and Love,
Slaxer13
22 2288
Hey Slaxer,
I'm loving the information you provided and also the full HTML tag.
Anyway, your download link points to "download.php", which doesn't surprise me that you keep downloading the incorrect file. Change the "download.php" to the path where the .txt file is. No need to create a download.php file, unless you want to keep track of the number of downloads, which I don't see you doing in the download.php file.
Hope that helps!
additionally, download.php corrupts your file by prepending HTML.
computerfox
I understand what you said and i will try as soon as its possible. There is just one thing i forgot to mention, i need to be able to download txt, word docs and PDF files.
Dormilich
Corrupts? I'm sorry i'm still new at this.
Thanks for the help so far ;)
Peace,
Slaxer13
Corrupts? I'm sorry i'm still new at this.
in downloads.php you always echo something on either line #8 or line #11 (which is the reason for the 'header already sent' notices later). and after that you echo the download content on line #24.
Can i make downloads of the files i mentioned above without the download.php? If yes how?
as computerfox already said, you could make a link pointing directly to the file.
can you exemplify? Sorry for the trouble.
- <a href="/downloads/example.txt">name</a>
There are going to be multiple files of multiple types. Can i refer to the contents of a folder instead of a specific file type?
There are going to be multiple files of multiple types.
make one link per file.
Can i refer to the contents of a folder instead of a specific file type?
if you refer to a folder, that folder with all its files is going to be displayed in the browser (unless—of course—the folder view is disabled in the server configuration).
All the files in the uploads folder are to visible and downloadble.
If you want to simply download a file, why are you going to a page to do so? What's the purpose?
If you provide the path to the file, it doesn't redirect to a useless page and also you can download multiple files without having to go back. When designing a site, it should also include a friendly feel and by adding that download page, it neglects to do that.
The download link is in a page where the user can see all the files in the folder. The user can see, upload and download files in the same page. And if is the admin he can also delete
Make this portion -
else{
-
while(list($id, $name) = mysql_fetch_array($result)){
-
echo "<a href=\"download.php?id=\$id\">$name</a><br>";
-
}
-
Something like -
else{
-
while(list($id, $name,$name) = mysql_fetch_array($result)){
-
$path=$basepath."/".$name;
-
echo "<a href='$path'>$name</a><br>";
-
}
-
Remove this portion -
if(isset($_GET['id'])){
-
$id = $_GET['id'];
-
$query = "SELECT file, type, size FROM tbl_uploads WHERE id = '$id'";
-
$result = mysql_query($query) or die('Error, query failed');
-
list($name, $type, $size, $content) = mysql_fetch_row($result);
-
header("Content-Disposition: attachment; filename=$name");
-
header("Content-type: $type");
-
header("Content-length: $size");
-
print $content;
-
It's not needed and also causes issues as it calls the page again.
Just to point out that the "download" link is just the link to the same page so it's not really a download link. Why are we so resistant to take our suggestions? Have you tried our fixes? Make a backup of the file, apply our fix, and then decide if you want to keep it.
where you have base path its where i put the path to the folder right?
the folder is in the root folder
So then you would remove $basepath?
Don't just copy/paste the code provided....
As a programmer you never assume, so in your case if $basepath is the root, you remove $basepath.
Also, if it's important to provide information of the file, that is normally done in the table where you have the list of files. How am I suppose to know what type of a file you're making me download?
ok i will try it at night and post the result. Thanks to both for all your help :)
Peace,
Slaxer13
Good luck and keep on coding!
Always ;)
Peace,
Slaxer13
I only now saw your post update. In my table there is that info about the file
Okay, great. How does the code look now? Are you providing the right path in the table?
The folder is in the root folder so i suppose this is what you told me to do - <?php
-
require 'dbconfig.php';
-
-
$query = "SELECT * FROM tbl_uploads";
-
$result = mysql_query($query) or die('Error, query failed');
-
-
if(mysql_num_rows($result)==0){
-
echo "Database is empty <br>";
-
}
-
else{
-
while(list($id, $name,$name) = mysql_fetch_array($result)){
-
$path="/uploads".$name;
-
echo "<a href='$path'>$name</a><br>";
-
}
-
}
-
?>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ilia Makarenko |
last post by:
Hi everyone!
I've got a problem with IIS 6.0 when downloading files with size > 4
Mb. So I am trying to download file in chunks, but it doesn't work.
Only pert of the file is downloaded (~6.5 of...
|
by: Micky |
last post by:
Hi,
I have a problem with System.Net.WebClient.DownloadFile() method used in an
aspx page.
The error is: The underlying connection was closed: Unable to connect to the
remote server.
If I...
|
by: user2008 |
last post by:
Hi all,
I want to track how many times visitor download file from my website,
for example, when visitor click on a download link, a ASPX page will be
requested, after that it will redirect to a...
|
by: seberino |
last post by:
How make a Python script
1. login
2. type password &
3. download file
all from a **remote web site**?
I'm not comfortable with *MY* software handling the password part.
|
by: bonita |
last post by:
If I add the code for user to download the file (e.g.
if(File.Exists(FILE_NAME)){......}), the ASP.NET will give the
following timeout error:
Timeout expired. The timeout period elapsed prior to...
|
by: matt |
last post by:
I have used some free code for listing files for download, but I want to
send an email to the administrator when the file has been downloaded. I
have got some code in here that does it, but it will...
|
by: BiT |
last post by:
Hello,
I'm working right now on project in vb.net 2005 for my company, i need the
project to download file from the company web site.
In order to get the file i have to give the site address...
|
by: manirajmurali |
last post by:
hi all,
i have face this problem more 3 days.. if anybody help me..
i have upload file through php coding .. i have download that same file from server..
its coming empty file.. i dont know...
|
by: vikassawant |
last post by:
Hi,
I m facing some critical problem for downloading 5mb file from server. Does anyone know?
How to reduce the time to download file from server using HTTP connection?
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |