php 保存远程文件.(mp3.mp4…)
<?php
//can run in background.
ignore_user_abort(true);
set_time_limit(0);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//获取提交的数据。资源链接和名字
$url = htmlspecialchars($_POST["url"]);
$srcname = htmlspecialchars($_POST["srcname"]);
//转变字符,"\" ,"/" , "?" 到 "-"
$srcname = str_replace("/","-",$srcname);//$str=str_replace("old","new",$str);
$srcname = str_replace("?","-",$srcname);
$srcname = str_replace("\\","-",$srcname);
//除去前后空格.
$srcname=chop($srcname);
$srcname=strrev($srcname);
$srcname=chop($srcname);
$srcname=strrev($srcname);
//获取内容
$txt = file_get_contents($url);
//定义保存路径 ,没有就创建 。
$dir = "download/";
if (!file_exists($dir)) {
mkdir($dir);
}
//定义保存为 文件名
$fname = tempnam("$dir" ,"download_") ;
//保存
$myfile = fopen("$fname", "a+") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
//作记录到$recorder文件中
$recorder = $dir."download-recorder.xml" ;
$txt2 = "<dt><url>$url</url> <fname>$fname</fname></dt>";
$myfile = fopen("$recorder", "a+") or die("Unable to open file!");
fwrite($myfile,$txt2 );
fclose($myfile);
//重命名文件。加上提交的资源名称。
//rename(oldname,newname,context) ;
rename($fname,"$fname"."_"."$srcname") ;
}
?>
<!--
一些用法:
$str=" bn <scipt>"."d cv v" ."dc ";
#strrev();
#htmlspecialchars($str);
#echo strip_tags("Hell?o <b><i>world!</i></b>","<b>");
#echo str_ireplace("WORLD","Peter","Hello world!");
#echo str_replace("world","Peter","Hello World!");
#echo "he" .chop(' Hello World! ') . "he<br>";
#echo "he" .rtrim(' Hello World! ') . "he<br>";
#echo chop('Hello World!','world!');
$str=htmlspecialchars($str);
$str=chop($str);
$str=strrev($str);
$str=chop($str);
$str=strrev($str);
echo $str;
-->
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>save-file</title>
</head>
<body >
<form method="post" action="">
输入资源url<input type="text" name="url" value=""><br>
输入资源名称<input type="text" name="srcname" value=""><br>
<input type="submit" value="提交">
</form>
</body>
</html>