使用file_get_contents下载内容,然后删除

时间:2016-11-23 21:11:33

标签: php

目前我发现此网站提供的音乐相关工具非常方便,它为文件主机提供镜像,我正在尝试使用file_get_contents下载一些MP3,然后将其删除后再推送到此文件主机镜像。

目前的代码就是这个..

$mp3 = 'http://www.example.com/song.mp3';
$name = 'song';
file_put_contents('tmp/'.$name.'.mp3',file_get_contents($mp3));

哪个会存储我们正在尝试收集的MP3,但在保存此信息之后我希望它通过此文件镜像上的功能运行我们找到API然后从我们的tmp中删除MP3文件之后的目录。

该网站是filemack

2 个答案:

答案 0 :(得分:3)

include('filemack.class.php');

// set path to temp directory
$temp_directory = dirname(__FILE__).'/tmp';

// set direct url to mp3
$mp3_url = 'https://www.filemack.com/embed/stream-demo.mp3';

// set name of mp3
$name = 'song';

// download file to temp directory
file_put_contents($temp_directory.'/'.$name.'.mp3',file_get_contents($mp3_url));

## start the filemack class
$filemack = new filemack;

## set your api key
$filemack->api_key = 'YOUR_API_KEY';

## set your api secret
$filemack->api_secret = 'YOUR_API_SECRET';

## enable individual hosts
$filemack->clicknupload = 1;
$filemack->dopefile = 1;

## or enable all of them    
$filemack->all();

## change embed color 1
$filemack->embed_color_1 = '375a7f';

## change embed color 2
$filemack->embed_color_2 = 'ffffff';

## upload the file
#$links = $filemack->upload($temp_directory.'/'.$name.'.mp3');

## upload the file with new filename
$links = $filemack->upload($temp_directory.'/'.$name.'.mp3',$name.'.mp3');

// file has been pushed to filemack so delete now
unlink($temp_directory.'/'.$name.'.mp3');

## links variable will be an array
Array
(
    [success] => 1
    [embed] => <iframe src="https://www.filemack.com/embed/YdugtL8sSzV5?c1=375a7f&c2=ffffff" frameborder="0" scrolling="none" width="100%" height="64"></iframe>
    [links] => Array
        (
        [filemack] => https://www.filemack.com/YdugtL8sSzV5
        [clicknupload] => https://www.filemack.com/cu_tQwdzijxCJpmOIqd
        [zippyshare] => https://www.filemack.com/zs_fL6CxCymd7EUe9Qh
        [dopefile] => https://www.filemack.com/df_rPNgoMH7pznIK6rq
        [affix] => https://www.filemack.com/af_c54jPe26dfcEc30a
        [tusfiles] => https://www.filemack.com/tf_RGtEhDrnlTMHsSKO
        [suprafiles] => https://www.filemack.com/sf_vm4g0FuRwXlxbBsP
    )

)

答案 1 :(得分:1)

只需使用unlink()

unlink('tmp/'.$name.'.mp3');