473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Download file problem

Slaxer13
106 64KB
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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include_once 'dbconfig.php';
  3. if(isset($_POST['btn-upload']))
  4. {    
  5.  
  6.     $file = rand(1000,100000)."-".$_FILES['file']['name'];
  7.     $file_loc = $_FILES['file']['tmp_name'];
  8.     $file_size = $_FILES['file']['size'];
  9.     $file_type = $_FILES['file']['type'];
  10.     $folder="uploads/";
  11.  
  12.     // new file size in KB
  13.     $new_size = $file_size/1024;  
  14.     // new file size in KB
  15.  
  16.     // make file name in lower case
  17.     $new_file_name = strtolower($file);
  18.     // make file name in lower case
  19.  
  20.     $final_file=str_replace(' ','-',$new_file_name);
  21.  
  22.     if(move_uploaded_file($file_loc,$folder.$final_file))
  23.     {
  24.         $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
  25.         mysql_query($sql);
  26.         ?>
  27.         <script>
  28.         alert('successfully uploaded');
  29.         window.location.href='view.php?success';
  30.         </script>
  31.         <?php
  32.     }
  33.     else
  34.     {
  35.         ?>
  36.         <script>
  37.         alert('error while uploading file');
  38.         window.location.href='upload.php?fail';
  39.         </script>
  40.         <?php
  41.     }
  42. }
  43. ?>
My Download.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     require 'dbconfig.php';
  3.  
  4.     $query = "SELECT * FROM tbl_uploads";
  5.     $result = mysql_query($query) or die('Error, query failed');
  6.  
  7.     if(mysql_num_rows($result)==0){
  8.         echo "Database is empty <br>";
  9.     }
  10.     else{
  11.         while(list($id, $name) = mysql_fetch_array($result)){
  12.             echo "<a href=\"download.php?id=\$id\">$name</a><br>";
  13.         }
  14.     }
  15.  
  16.     if(isset($_GET['id'])){
  17.         $id    = $_GET['id'];   
  18.         $query = "SELECT file, type, size FROM tbl_uploads WHERE id = '$id'";       
  19.         $result = mysql_query($query) or die('Error, query failed');
  20.         list($name, $type, $size, $content) =  mysql_fetch_row($result);
  21.         header("Content-Disposition: attachment; filename=$name");
  22.         header("Content-type: $type");
  23.         header("Content-length: $size");
  24.         print $content;
  25.     }
  26. ?>
My view.php (where the download link is)

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include_once 'dbconfig.php';
  3. if(isset($_GET['remove_id']))
  4. {
  5.     $res=mysql_query("SELECT file FROM tbl_uploads WHERE id=".$_GET['remove_id']);
  6.     $row=mysql_fetch_array($res);
  7.     mysql_query("DELETE FROM tbl_uploads WHERE id=".$_GET['remove_id']);
  8.     unlink("uploads/".$row['file']);
  9.     header("Location: view.php");
  10. }
  11. ?>
  12. <!DOCTYPE html>
  13. <html xmlns="http://www.w3.org/1999/xhtml">
  14. <head>
  15. <title>Delete Uploaded Files From Folder in PHP</title>
  16. <link rel="stylesheet" href="style.css" type="text/css" />
  17. <script type="text/javascript">
  18. function remove(id)
  19. {
  20.     if(confirm(' Sure to remove file ? '))
  21.     {
  22.         window.location='delete.php?remove_id='+id;
  23.     }
  24. }
  25. </script>
  26. </head>
  27. <body>
  28. <div id="header">
  29. <label>Delete Uploaded Files From Folder in PHP</label>
  30. </div>
  31. <div id="body">
  32.     <table width="80%" border="1">
  33.     <tr>
  34.     <th colspan="4">your uploads...<label><a href="relatorios.php">upload new files...</a></label></th>
  35.     </tr>
  36.     <tr>
  37.     <td>File Name</td>
  38.     <td>File Type</td>
  39.     <td>File Size(KB)</td>
  40.     <td>View</td>
  41.     <td>Delete</td>
  42.     <td>Download</td>
  43.         <?php
  44.     $sql="SELECT * FROM tbl_uploads";
  45.     $result_set=mysql_query($sql);
  46.     while($row=mysql_fetch_array($result_set))
  47.     {
  48.         ?>
  49.     </tr>
  50.  
  51.         <tr>
  52.         <td><?php echo $row['file'] ?></td>
  53.         <td><?php echo $row['type'] ?></td>
  54.         <td><?php echo $row['size'] ?></td>
  55.         <td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">view file</a></td>
  56.         <td><a href="javascript:remove(<?php echo $row['id'] ?>)">Delete file</a></td>
  57.         <td><a href="download.php">Download</a></td>
  58.       </tr>
  59.         <?php
  60.  
  61.     }
  62.     ?>
  63.     </table>
  64.  
  65. </div>
  66. </body>
  67. </html>
Thaks in advance.
Peace and Love,

Slaxer13
Attached Images
File Type: jpg The tbl_uploads table.jpg (18.3 KB, 100 views)
File Type: jpg The txt file....jpg (7.8 KB, 90 views)
May 21 '15 #1
22 2288
computerfox
276 100+
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!
May 21 '15 #2
Dormilich
8,658 Expert Mod 8TB
additionally, download.php corrupts your file by prepending HTML.
May 22 '15 #3
Slaxer13
106 64KB
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
May 22 '15 #4
Dormilich
8,658 Expert Mod 8TB
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.
May 22 '15 #5
Slaxer13
106 64KB
Can i make downloads of the files i mentioned above without the download.php? If yes how?
May 22 '15 #6
Dormilich
8,658 Expert Mod 8TB
as computerfox already said, you could make a link pointing directly to the file.
May 22 '15 #7
Slaxer13
106 64KB
can you exemplify? Sorry for the trouble.
May 22 '15 #8
Dormilich
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. <a href="/downloads/example.txt">name</a>
May 22 '15 #9
Slaxer13
106 64KB
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?
May 22 '15 #10
Dormilich
8,658 Expert Mod 8TB
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).
May 22 '15 #11
Slaxer13
106 64KB
All the files in the uploads folder are to visible and downloadble.
May 22 '15 #12
computerfox
276 100+
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.
May 22 '15 #13
Slaxer13
106 64KB
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
May 22 '15 #14
computerfox
276 100+
Make this portion
Expand|Select|Wrap|Line Numbers
  1.     else{
  2.         while(list($id, $name) = mysql_fetch_array($result)){
  3.             echo "<a href=\"download.php?id=\$id\">$name</a><br>";
  4.         }
  5.  
Something like
Expand|Select|Wrap|Line Numbers
  1.     else{
  2.         while(list($id, $name,$name) = mysql_fetch_array($result)){
  3.             $path=$basepath."/".$name;
  4.             echo "<a href='$path'>$name</a><br>";
  5.         }
  6.  
Remove this portion
Expand|Select|Wrap|Line Numbers
  1.     if(isset($_GET['id'])){
  2.         $id    = $_GET['id'];   
  3.         $query = "SELECT file, type, size FROM tbl_uploads WHERE id = '$id'";       
  4.         $result = mysql_query($query) or die('Error, query failed');
  5.         list($name, $type, $size, $content) =  mysql_fetch_row($result);
  6.         header("Content-Disposition: attachment; filename=$name");
  7.         header("Content-type: $type");
  8.         header("Content-length: $size");
  9.         print $content;
  10.  
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.
May 22 '15 #15
Slaxer13
106 64KB
where you have base path its where i put the path to the folder right?
the folder is in the root folder
May 22 '15 #16
computerfox
276 100+
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?
May 22 '15 #17
Slaxer13
106 64KB
ok i will try it at night and post the result. Thanks to both for all your help :)

Peace,
Slaxer13
May 22 '15 #18
computerfox
276 100+
Good luck and keep on coding!
May 22 '15 #19
Slaxer13
106 64KB
Always ;)

Peace,
Slaxer13
May 22 '15 #20
Slaxer13
106 64KB
I only now saw your post update. In my table there is that info about the file
May 25 '15 #21
computerfox
276 100+
Okay, great. How does the code look now? Are you providing the right path in the table?
May 25 '15 #22
Slaxer13
106 64KB
The folder is in the root folder so i suppose this is what you told me to do

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     require 'dbconfig.php';
  3.  
  4.     $query = "SELECT * FROM tbl_uploads";
  5.     $result = mysql_query($query) or die('Error, query failed');
  6.  
  7.     if(mysql_num_rows($result)==0){
  8.         echo "Database is empty <br>";
  9.     }
  10.      else{
  11.         while(list($id, $name,$name) = mysql_fetch_array($result)){
  12.             $path="/uploads".$name;
  13.             echo "<a href='$path'>$name</a><br>";
  14.         }
  15.     }
  16. ?>
May 26 '15 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
0
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...
1
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...
7
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.
0
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...
16
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...
8
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...
2
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...
1
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?
0
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...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
0
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...
1
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...
0
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
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.