Php 正在将远程url上载到服务器
我使用以下代码将远程文件上载到我的服务器。它在提供直接下载链接的地方非常有效,但最近我注意到,很少有网站提供mysql链接作为下载链接,当我们点击该链接时,文件开始下载到我的电脑。但即使在该页面的html源代码中,它也不显示直接链接 这是我的密码:Php 正在将远程url上载到服务器,php,url,Php,Url,我使用以下代码将远程文件上载到我的服务器。它在提供直接下载链接的地方非常有效,但最近我注意到,很少有网站提供mysql链接作为下载链接,当我们点击该链接时,文件开始下载到我的电脑。但即使在该页面的html源代码中,它也不显示直接链接 这是我的密码: <form method="post"> <input name="url" size="50" /> <input name="submit" type="submit" /> </form>
<form method="post">
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>
<?php
if (!isset($_POST['submit'])) die();
$destination_folder = 'mydownloads/';
$url = $_POST['url'];
$newfname = $destination_folder . basename($url);
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
?>
它适用于所有直接下载链接的链接,例如,如果我愿意
link它将上传音乐文件,但文件名为48.php?id=415508,但实际的mp3文件存储在因此,如果我能得到实际的目的地url,名称将是Appy_Budday_uuz(Videshi)-Santokh_Singh(www.Mzc.in).mp3
所以我想获得实际的下载url。问题是原始url正在重定向。如果要捕获它被重定向到的URL,请尝试使用标题,然后获取basename($redirect\u URL)作为文件名 +1为Robbie使用卷发 如果运行(从命令行) 您可以在这里看到位置标题是新的url 在php中,请尝试以下操作
$ch = curl_init('http://priceinindia.org/muzicpc/48.php?id=415508');
curl_setopt($ch, CURLOPT_HEADER, 1); // return header
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // dont redirect
$c = curl_exec($ch); //execute
echo curl_getinfo($ch, CURLINFO_HTTP_CODE); // will echo http code. 302 for temp move
echo curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // url being redirected to
您希望找到标题的位置部分。不确定设置,但我确定
编辑3..还是4?
是的,我知道发生了什么。实际上,您希望跟踪位置url,然后在不下载文件的情况下回显有效url。试试看
$ch = curl_init('http://priceinindia.org/muzicpc/48.php?id=415508');
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$c = curl_exec($ch); //execute
echo curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // url being redirected to
当我运行这个程序时,我的输出是
[username@localhost ~]$ php test.php
http://lq.mzc.in/data48-2/37202/Appy_Budday_(Videshi)-Santokh_Singh(www.Mzc.in).mp3
为此,您应该使用Curl库 手册(在该链接上)提供了一个如何使用curl的示例。在关闭连接之前,请调用curl\u getinfo(http://php.net/manual/en/function.curl-getinfo.php)特别是获取你想要的有效URL
<?php
// Create a curl handle
$ch = curl_init('http://www.yahoo.com/');
// Execute
$fileData = curl_exec($ch);
// Check if any error occured
if(!curl_errno($ch)) {
$effectiveURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
// Close handle
curl_close($ch);
?>
(您也可以使用curl直接写入文件-使用CURLOPT_文件选项。也在手册中)我添加了echo$effectiveURL;但它只返回输入url,而不是重定向的urldone。您应该习惯于从命令行执行curl之类的命令。它在调试时非常有用。看起来Robbie有这个设置。。。curl\u getinfo($ch,CURLINFO\u有效\u URL);我得到这个作为输出HTTP/1.1 302临时移动服务器:nginx/1.0.10日期:Wed,Sep 2012 07:49:21 GMT内容类型:text/html传输编码:chunked连接:keep alive X-Powered-By:PHP/5.3.10位置:302我只想要URL当您构建文件名时检查curl\u getinfo($ch,CURLINFO\u HTTP\u CODE)!=301或302以其他方式获取curl\u getinfo($ch,CURLINFO\u EFFECTIVE\u URL)并从中构建文件名。
<?php
// Create a curl handle
$ch = curl_init('http://www.yahoo.com/');
// Execute
$fileData = curl_exec($ch);
// Check if any error occured
if(!curl_errno($ch)) {
$effectiveURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
// Close handle
curl_close($ch);
?>