doujiu8479
doujiu8479
2015-11-28 01:44
浏览 26
已采纳

如何从我的下载名称中删除tmp-

So when a user downloads a file I currently have it pull the song name through something that looks like this.

$songs = file_get_contents('https://api.soundcloud.com/tracks/'.$id.'/stream?client_id='.$myID.'');
$filename = './tmp/' . stripslashes(htmlspecialchars($songTitle)) . '.mp3';
$filenames = '/tmp/' . stripslashes(htmlspecialchars($songTitle)) . '.mp3';
$trying = '' . stripslashes(htmlspecialchars($songTitle)) . '';
$songname = stripslashes(htmlspecialchars($songTitle));
file_put_contents($filename, $songs);


<form method="POST" action="http://example.com/download.php" accept-charset="utf-8">
    <input type="hidden" name="file_name" value="<?php print $trying; ?>" />
    <input type="hidden" name="song_name" value="<?php print $songname; ?>" />
    <input type="Submit"  class="btn btn-success" value="Download Song" /> 
</form>

Then within the download.php file I have the following.

$file = 'tmp/' . $_POST['file_name'] . '.mp3';

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$file);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header("Content-Type: audio/mp3");
readfile($file);

How ever when a user downloads a file it brings up something that looks like this tmp-song-artist-song-name.mp3 any help is very appreciated!

Edit:

Preview Image

This is what happens when I change the header('Content-Disposition: attachment; filename='.$file); line to header('Content-Disposition: attachment; filename='.$songtitle);

$songtitle = $_POST['song_name'];

It seems to fix the title issue but creates an entirely new issue which is not what we want.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongxi3859
    dongxi3859 2015-11-28 02:04
    已采纳

    You are using wrong Content-Disposition header. In your first post you have used full file name alongwith tmp/ and after editing you missded .mp3 at end. I have corrected your download file code below:

    $file = 'tmp/' . $_POST['file_name'] . '.mp3';
    
    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename='.$_POST['file_name'] . '.mp3');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    header("Content-Type: audio/mp3");
    readfile($file);
    
    点赞 评论
  • dongque20030402
    dongque20030402 2015-11-28 02:11

    Remove 'tmp/' section from file name.

    header('Content-Disposition: attachment; filename=' . str_replace('tmp/', '', $file));
    
    点赞 评论

相关推荐