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
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.
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]"
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.
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>
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
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.
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....
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.
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...
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: