473,396 Members | 1,843 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,396 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 2297
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.