SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: File Type Query

  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File Type Query

    Hi Guys,

    i'm using this code to download specific files from my server:

    PHP Code:
    <button onclick="window.location='www.location.com/of/file'">Some Text</button
    it works great for .zip/.rar files but when i try to download a .xml file, instead of the download dialogue box popping up, it reads the file instead of downloading it, is there any way i can fix this?

    thanks guys

    Graham

  2. #2
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will have to modify header information of your xml files here.

    See this

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    header('Content-Type: text/xml');
    header('Content-Disposition: attachment; filename=sample.xml');
    readfile('sample.xml'); 
    my mobile portal
    ghiris.ro

  4. #4
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guys,

    strange it's still readinging the xml file instead of downloading heres my download code:

    PHP Code:
         $xml_id $_GET['id'];    
         
         
    // get the nzb...///////////////////////////////////////////////////////////////////
         
    $query1 "SELECT * FROM `uploaded_xmls` WHERE `id`='$xml_id'"
         
    $result1 mysql_query($query1);
         
    $row mysql_fetch_array($result1);     
         
    $xml $row['xml_file']; 
         
    $xml_name $row['file_name'];
         
         
    // the logged in users id...////////////////////////////////////////////////////////
         
    $query2 "SELECT * FROM `membership` WHERE `username`='$member'";
         
    $result2 mysql_query($query2);
         
    $rows mysql_fetch_array($result2);
         
    $users_id $rows['id'];
         
         
    // if the user hasn't already been entered then...//////////////////////////////////
         
    $check_query "SELECT * FROM `whos_downloaded` WHERE `nzb_id`='$xml_id' AND `downloader_id`='$users_id'";
         
    $check_result mysql_query($check_query);
         
         if(
    mysql_num_rows($check_result) < 1) {
         
         
    // insert the details into mysql.../////////////////////////////////////////////////
         
    $query3 "INSERT INTO `whos_downloaded` (`nzb_id`,`downloader_id`) VALUES ('$xml_id','$users_id')";
         
    $result3 mysql_query($query3);     
         
         }
         
         
    // update the hit counter...////////////////////////////////////////////////////////
         
    mysql_query("UPDATE `uploaded_xmls` SET `downloads`=`downloads` + 1 WHERE `id`='$xml_id'");
         
         echo 
    '<br /><b>('.$xml_name.')<br /><br />';
         echo 
    '>>> <a href="uploads/'.$xml.'"><font color="red">'.$xml.'</font></a> <<<</b><br /><br />';
         
         echo 
    " <button onclick=\"window.location='uploads/$xml'\">Some Text</button> "
         
         
    ## download ########################################################################
         
    header('Content-Type: text/xml');
         
    header('Content-Disposition: attachment; filename=uploads/'.$xml.'');
         
    readfile('uploads/'.$xml.'');
         
    ## download ######################################################################## 
    thanks guys

    Graham

  5. #5
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should link to a PHP file that contains the code posted by Ernie1, instead of directly linking to the file.
    Example:

    Code HTML:
    <a href="myfile.php?id=1">Download my file!</a>

    Code PHP:
    // inside myfile.php
    $myfile = /* get XML file with id = $_GET['id'] */
     
    header('Content-Type: text/xml');
    header('Content-Disposition: attachment; filename=sample.xml');
    readfile($myfile);

    Also, it strikes me as odd that you use Javascript to access this file. Don't you want people that have Javascript disabled to be able to access your file?

  6. #6
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guys,

    i have edited the code very slightly to this as my myfile.php

    PHP Code:
    <?php
     
    // the includes...//////////////////////////////////////////////////////////////////
     
    include("includes/db_connection.php");
     include(
    "includes/constants.php");
     
     
    $xml_id $_GET['id'];
     
     
    // get the xml...///////////////////////////////////////////////////////////////////
     
    $query1 "SELECT * FROM `uploaded_xmls` WHERE `id`='$xml_id'"
     
    $result1 mysql_query($query1);
     
    $row mysql_fetch_array($result1);     
     
    $xml $row['xml_file']; 
     
     
    header('Content-Type: text/xml');
     
    header('Content-Disposition: attachment; filename="uploads/'.$xml.'"');
     
    readfile("uploads/$xml"); 
    ?>
    is there a way to have 2 or more contect types? would i just , add , add?

    also is there a way above i could append a different extension onto one of the files before it downloads?

    thanks a lot guys

    Graham


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •