Download file problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Slaxer13
    New Member
    • Jun 2014
    • 106

    Download file problem

    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

    Code:
    <?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

    Code:
    <?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)

    Code:
    <?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
    Attached Files
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    Hey Slaxer,

    I'm loving the information you provided and also the full HTML tag.

    Anyway, your download link points to "download.p hp", which doesn't surprise me that you keep downloading the incorrect file. Change the "download.p hp" 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!

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      additionally, download.php corrupts your file by prepending HTML.

      Comment

      • Slaxer13
        New Member
        • Jun 2014
        • 106

        #4
        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

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          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.

          Comment

          • Slaxer13
            New Member
            • Jun 2014
            • 106

            #6
            Can i make downloads of the files i mentioned above without the download.php? If yes how?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              as computerfox already said, you could make a link pointing directly to the file.

              Comment

              • Slaxer13
                New Member
                • Jun 2014
                • 106

                #8
                can you exemplify? Sorry for the trouble.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  Code:
                  <a href="/downloads/example.txt">name</a>

                  Comment

                  • Slaxer13
                    New Member
                    • Jun 2014
                    • 106

                    #10
                    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?

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      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).

                      Comment

                      • Slaxer13
                        New Member
                        • Jun 2014
                        • 106

                        #12
                        All the files in the uploads folder are to visible and downloadble.

                        Comment

                        • computerfox
                          Contributor
                          • Mar 2010
                          • 276

                          #13
                          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.

                          Comment

                          • Slaxer13
                            New Member
                            • Jun 2014
                            • 106

                            #14
                            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

                            Comment

                            • computerfox
                              Contributor
                              • Mar 2010
                              • 276

                              #15
                              Make this portion
                              Code:
                                  else{
                                      while(list($id, $name) = mysql_fetch_array($result)){
                                          echo "<a href=\"download.php?id=\$id\">$name</a><br>";
                                      }
                              Something like
                              Code:
                                  else{
                                      while(list($id, $name,$name) = mysql_fetch_array($result)){
                                          $path=$basepath."/".$name;
                                          echo "<a href='$path'>$name</a><br>";
                                      }
                              Remove this portion
                              Code:
                                  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.
                              Last edited by computerfox; May 22 '15, 02:18 PM. Reason: More details.

                              Comment

                              Working...