#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5

    Changing a downloadable zip file to pdf file?


    Hello All,

    We have a system in place that allows people to download zip files, but i was wondering if the following code could be amended to allow by to download either zip files or pdfs.

    PHP Code:
    <?PHP
        session_start
    ();
        
        function 
    custom_error($error){
            switch(
    $error){
                case 
    "Error1":
                    echo 
    $error." Cannot connect.";    
                    break;
                
                case 
    "Error2":
                    echo 
    $error." Cannot connect to database.";    
                    break;
                    
                case 
    "Error3":
                    echo 
    $error." Cannot find resource.";    
                    break;
                    
                case 
    "Error4":
                    echo 
    $error." Problems retrieving resource.";    
                    break;
                
                case 
    "Error5":
                    echo 
    $error." Problems finding your actual resource.";    
                    break;
            }
            
            echo 
    "Please contact us <a href='mailto:joe.bloggd@home.uk'>here</a><br>or visit <a href='http://www.home.uk' target='_blank'>home</a>";
        }
        
        if (!
    $cn mysql_connect("USERNAME""XXXXXX""XXXXX")) 
            die(
    custom_error("Error1"));
            
        if (!
    mysql_select_db("design"$cn)) 
            die(
    custom_error("Error2"));
        
        if (!
    $rs1 mysql_query("SELECT * FROM resources WHERE title= '" $_POST["title"]."'")) 
            die(
    custom_error("Error3"));
            
        if (!
    $rd1 mysql_fetch_array($rs1)) 
            die(
    custom_error("Error4"));
        
        
    $file_name $_POST["title"].".zip";
        
    $url $rd1["filepath"].$file_name;
        if(
    $_POST["username"]){
            
    $username $_POST["username"];
        }else{
            
    $username "Anonymous";    
        }
        
    $file_size filesize("../" $url);
        if(
    $file_size <= ){
            die(
    custom_error("Error5"));
        }

        
        
    mysql_query("INSERT INTO db_one VALUES('', '".date('Y-m-d H:i:s')."', '".$username."', '".$file_name."', '".$_SERVER['HTTP_REFERER']."')");
        
    mysql_close($cn);
        
        
    header("Content-disposition: attachment; filename=" $file_name);
        
    header("Content-type: application/octet-stream");
        
    header("Content-length: " .$file_size);
        
        
    $zipfile fopen ("../" $url"r");
        
    $block fread($zipfile1024);
        while (
    strlen($block) != 0)
        {
            echo 
    $block;
            
    flush();
            
    $block fread($zipfile1024);
        }
        
    fclose ($zipfile);
            
    ?>
    Im very new to php, and was hoping for some guidance on this.

    Can i simply amend this:

    PHP Code:
    $file_name $_POST["title"].".zip"
    to something like this

    PHP Code:
    $file_name $_POST["title"].".zip"".pdf"
    to get the desired result?

    Any help would be greatly appreciated

    Regards
    MG
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,457
    Rep Power
    594
    It doesn't work that way. That code is looking for a file with a name of whatever was entered in the form for 'title' and forcing an extension of zip.The result is an explicit file name (foo.zip) where just foo was input. Your code would get a syntax error. The user would need to specify the full name in the input field including the extension (foo.zip or foo.pdf) and then you set '$filename' to '$_POST' without appending the extension.

    In addition you would need to modify the headers (Content-Type) to indicate pdf or zip depending on which you were uploading.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5
    Hi gw, thanks for the reply,

    Sorry I should have mentioned this before. The file names have been uploaded to a database along with their location. eg:

    id = X
    file name = Making_an_Impact
    file location = ../resources/age-range/


    This is an example of the front end code.

    Code:
    <div class="res">
             <div class="avail_image"><img src="/images/Making_an_Impact.jpg"  
    alt="image" border="0" /></div>
         <div class="avail_text">
          <h3>Making an Impact</h3>
          <h4 class="age">11-14 years</h4>
         <p>The West Midlands Digital Support Centre</p>
          <h4>Issue Number:</h4>
         <ul>
         <li>D&amp;T Practice 5.2011</li>
         </ul>
         <?php dload('Making_an_Impact'); ?>
          </div>
        <div style="clear: both;"></div>
    </div>
    As you can see, the "php dload" has the name of the zipped file, minus the .zip extension, this is the case for all files. they have many options to pick from, and once they have done, the can download the file.

    This is why i assumed it would be ok to add the .pdf.

    Im not sure if this changes anything from your original reply, and again, sorry for not mentioning this before.

    Ill re-read the reply, in case ive missed something.

    Regards
    MG
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,457
    Rep Power
    594
    It doesn't change my reply much. You still need to determine if a zip or pdf is to be uploaded to get the correct file name and to output the correct headers.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5
    Thanks for the quick reply,

    please forgive the lack of knowledge, but is it not possible with the first php code then - to have both .zips and .pdf?

    is it possible just to have pdfs?

    i guess i could leave it as it currently is.

    Regards
    MG
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,457
    Rep Power
    594
    No. Your first code does not produce the correct file name. The name must be either foo.zip or foo.pdf not foo.zip.pdf. What you can do is put the full file name in the database rather than just the name or add a column that indicates a zip or pdf, then create the name and headers based on that. If you are looking for a one line change, there isn't one. You are going to have to add code to handle the different file types differently. And remember you still need to use different headers for the different types.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    As gw1500se mentioned, you could add a new column to your database table which specifies the format for the file. For example, if you named this column file_type and gave it possible values of zip or pdf, the only line you would have to change is:
    PHP Code:
    $file_name $_POST["title"].".zip"
    PHP Code:
    $file_name $_POST["title"] . $rd1['file_type']; 
    Your .pdf files would be placed in the same place as your .zip files. I assume you would use separate titles for each format then, and present separate download links.

    The file will always be downloaded though unless you were to change the headers. If you serve the file as a pdf instead of an octet-stream it would allow the browser to view the PDF inline.


    PHP Code:
    mysql_query("SELECT * FROM resources WHERE title= '" $_POST["title"]."'"
    This is dangerous because it allows for SQL injection exploits. Escape all variables used in queries using mysql_real_escape_string.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5
    Hello,

    gw1500se and E-oreo thanks for the reply,

    The vast majority of files (about 90%) are pdfs, but I've had to zip them up in order for them to be downloadable - because of this download.php code. The remaining 10% comprise of different files (word, ppt and pdfs), and so had to be zipped.

    E-oreo: I'll amend the file as you suggested and see where it takes me

    not entirely sure what you mean by:

    "The file will always be downloaded though unless you were to change the headers."

    Are you saying that unless a change in the header is made then people will be able to download both the zip and the pdf? If this is the case, then i dont think it'd mater all that much, as they at least have a choice of the zip or pdf. if this isnt what you mean, sorry i missed the point, could you please expand

    And I don't understand what you guys mean by "headers/different headers for different file types", can you point me in the right direction where i may find information please. what are headers etc.

    Regards
    MG
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,457
    Rep Power
    594
    E-Oreo and I were referring to these headers in your code:
    PHP Code:
    header("Content-disposition: attachment; filename=" $file_name);
    header("Content-type: application/octet-stream");
    header("Content-length: " .$file_size); 
    By using octet-stream it will always download the file regardless of the file type but the user will have to download the file before opening it. If you use explicit file types (particularly for PDFs) then the file can be automatically opened in a pdf reader when downloaded, without even being prompted if the user has it configured that way on their browser. With E-Oreo's suggestion you now have a variable indicating the file type so you could do something like this:
    PHP Code:
    header("Content-disposition: attachment; filename=" $file_name);
    if (
    $rd1['file_type']=="zip") {
       
    header("Content-type: application/zip");
    }
    else {
        
    header("Content-type: application/pdf");
    }
    header("Content-length: " .$file_size); 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo