simple syntax error - but I can't figure it out

Discussion in 'PHP' started by tyrone, Jun 10, 2010.

  1. #1
    I am trying to make a file download automatically when the user visits the page. The filename is the same as the pagename - but without the suffix.

    eg
    filename test.mp3
    pagename test.mp3.php

    The download page and pagename is automatically generated by another php when User1 uploads a file.

    Everything works when the syntax is simply:

    $filename = "test.mp3";

    But this is the actual variable I'm using to get the file:

    $filename = substr($html_file_name, 0, -4);

    My problem is that when user2 goes to the page the download is
    test.mp3[1]
    and not
    test.mp3

    I think this is simple but I have spent a whole day trying to crack this. Any help very gratefully received

    T
     
    tyrone, Jun 10, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    OK you need to learn to debug.

    First thing you need to do is see what is actually in $html_file_name. So before you do the substr echo $html_file_name. Post the result here and we go from there.
     
    stephan2307, Jun 10, 2010 IP
  3. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks a lot - trying this now
     
    tyrone, Jun 10, 2010 IP
  4. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    same problem I'm afraid:

    code now reads:

    echo $html_file_name;

    $filename = substr($html_file_name, 0, -4);

    still gets:
    "file download test.mp3[1]"
     
    tyrone, Jun 10, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    The code you posted should work, assuming $html_file_name is structured correctly.

    E.G If $html_file_name = hello.mp3.php then your code will return hello.mp3.

    What is the contents of $html_file_name? How aer you populating it - its probably an error when you assign the value. print the contents and see what it contains first.

    Include your full script, and it should be a simple fix.
     
    lukeg32, Jun 10, 2010 IP
  6. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    HERE YOU GO:

    <html>
    <head>
    <title>MP3 download</title>
    </head>
    <body>
    Hello name, here is your mp3.<br /><br />

    <?php

    echo $html_file_name;

    $filename = substr($html_file_name, 0, -4);
    $filesize = filesize($filename);
    $filetype = substr($filename, -3, 3);

    switch($filetype) {
    case "pdf": $mime="application/pdf"; break;
    case "zip": $mime="application/zip"; break;
    case "doc": $mime="application/msword"; break;
    case "xls": $mime="application/vnd.ms-excel"; break;
    case "ppt": $mime="application/vnd.ms-powerpoint"; break;
    case "gif": $mime="image/gif"; break;
    case "png": $mime="image/png"; break;
    case "jpg": $mime="image/jpg"; break;
    case "mp3": $mime="audio/mpeg"; break;
    case "wav": $mime="audio/x-wav"; break;
    case "mpg":
    case "mpe": $mime="video/mpeg"; break;
    case "mov": $mime="video/quicktime"; break;
    case "avi": $mime="video/x-msvideo"; break;

    default : $mime="application/octet-stream"; break;
    }

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Transfer-Encoding: binary");

    header("Content-Type: {$mime}");
    header("Content-Length: {$filesize}");
    header("Content-Disposition: attachment; filename=\"{$filename}\";");

    @readfile($filename);

    ?>

    switch($filetype) {
    case "pdf": $mime="application/pdf"; break;
    case "zip": $mime="application/zip"; break;
    case "doc": $mime="application/msword"; break;
    case "xls": $mime="application/vnd.ms-excel"; break;
    case "ppt": $mime="application/vnd.ms-powerpoint"; break;
    case "gif": $mime="image/gif"; break;
    case "png": $mime="image/png"; break;
    case "jpg": $mime="image/jpg"; break;
    case "mp3": $mime="audio/mpeg"; break;
    case "wav": $mime="audio/x-wav"; break;
    case "mpg":
    case "mpe": $mime="video/mpeg"; break;
    case "mov": $mime="video/quicktime"; break;
    case "avi": $mime="video/x-msvideo"; break;

    default : $mime="application/octet-stream"; break;
    }

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Transfer-Encoding: binary");

    header("Content-Type: {$mime}");
    header("Content-Length: {$filesize}");
    header("Content-Disposition: attachment; filename=\"{$filename}\";");

    @readfile($filename);

    ?>
    <br />
    </body>
     
    tyrone, Jun 10, 2010 IP
  7. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    and the file is named
     
    tyrone, Jun 10, 2010 IP
  8. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #8
    How do you get the test.mp3.php file into $html_file_name?
     
    stephan2307, Jun 10, 2010 IP
  9. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    There is currently an action.php that raeds like this
    That's fed by an input.php - (but that is temporary - I intend to feed the variable from Flash)
    the action.php takes a template "profile.php" and changes it's name. I tried using str_replace to overwrite the
    $filename =
    line in the profile.php but couldn't get that working so have reverted to this

    I very much appreciate your input
     
    tyrone, Jun 10, 2010 IP
  10. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #10
    OK I strongly suspect that the [1] comes from the post. Do a print_r($_POST); and check what the value for $_POST['name'] is. if it is still with [1] at the end then you need to check the file from where you send the post.
     
    stephan2307, Jun 10, 2010 IP
  11. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    nope the value doesn't have the [1]
    I have got ot go to a half hour meeting thanks so much again
     
    tyrone, Jun 10, 2010 IP
  12. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #12
    Is there any other code that handles $_POST['name'] in any way.
     
    stephan2307, Jun 10, 2010 IP
  13. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    This is the input form:
     
    tyrone, Jun 10, 2010 IP
  14. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    This thought occurred: that if the string including the [1] is not in the $html_file_name it must have something to do with the output of:
    $filename = substr($html_file_name, 0, -4);

    rather than the input....
     
    tyrone, Jun 10, 2010 IP
  15. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #15
    Nope as you echoed $html_file_name before you did the substr and it still had the [1].
    maybe try this
    just before you do the substr manually set the $html_file_name and see if it still work.
     
    stephan2307, Jun 10, 2010 IP
  16. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    sorry stephan - what is the code for that?
     
    tyrone, Jun 10, 2010 IP
  17. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #17
    $html_file_name = 'test.mp3.php';
     
    stephan2307, Jun 10, 2010 IP
  18. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    right the following:
    $html_file_name = 'test.mp3.php';


    $filename = substr($html_file_name, 0, -4);

    does produce the right result -
    so that's where the problem is.

    As you understand that the string must be overwritten on the template each time the script runs and I'm not clear how that can be achieved...
     
    tyrone, Jun 10, 2010 IP
  19. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #19
    no the problem is that $_POST['name'] seems to get polluted.

    do me another favour.

    remove the $html_file_name = 'test.mp3.php'; and place the following code there instead
    
    echo gettype($html_file_name);
    
    PHP:
     
    stephan2307, Jun 10, 2010 IP
  20. tyrone

    tyrone Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    this produces the same [1] result

     
    tyrone, Jun 10, 2010 IP