将远程 url 上传到服务器 [英] uploading remote url to server

查看:46
本文介绍了将远程 url 上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将远程文件上传到我的服务器.在提供直接下载链接的地方效果很好,但最近我注意到很少有网站将 mysql 链接作为下载链接,当我们单击该链接时,文件开始下载到我的电脑.但即使在该页面的 html 源代码中,它也没有显示直接链接.

I am using following codes to upload remote files to my server. It works great where direct download link is given but recently I have noticed that few websites are giving mysql links as download link and when we click on that link the files start downloading to my pc. But even in html source of that page it does not show the direct link.

这是我的代码:

 <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);
}

?>

它适用于所有下载链接是直接的链接,例如,如果我会给http://priceinindia.org/muzicpc/48.php?id=415508链接它将上传音乐文件,但文件名将是 48.php?id=415508 但实际的 mp3 文件存储在
http://lq.mzc.in/data48-2/37202/Appy_Budday_(Videshi)-Santokh_Singh(www.Mzc.in).mp3

It works great for all links where the download link is direct for example if I will give http://priceinindia.org/muzicpc/48.php?id=415508 link it will upload the music file but the file name will be 48.php?id=415508 but the actual mp3 file is stored at
http://lq.mzc.in/data48-2/37202/Appy_Budday_(Videshi)-Santokh_Singh(www.Mzc.in).mp3

所以如果我能得到实际的目标网址,名字将是 Appy_Budday_(Videshi)-Santokh_Singh(www.Mzc.in).mp3

So if I can get the actual destination url the name will be Appy_Budday_(Videshi)-Santokh_Singh(www.Mzc.in).mp3

所以我想得到实际的下载地址.

So I want to get the actual download url.

推荐答案

问题是原始 URL 正在重定向.您想捕获它被重定向到的 URL,尝试使用标头,然后获取 basename($redirect_url) 作为您的文件名.

The problem is the original URL is redirecting. You want to catch the URL it is being redirected to, try using the headers and then get the basename($redirect_url) as your file name.

Robbie 使用 CURL +1.

+1 for Robbie using CURL.

如果你运行(从命令行)

If you run (from command line)

[username@localhost ~]$ curl http://priceinindia.org/muzicpc/48.php?id=415508 -I
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.0.10
Date: Wed, 19 Sep 2012 07:31:18 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.10
Location: http://lq.mzc.in/data48-2/37202/Appy_Budday_(Videshi)-Santokh_Singh(www.Mzc.in).mp3

您可以在此处看到位置标题是新网址.

You can see the location header here is the new url.

在 php 中尝试类似

in php try something like

$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

您要查找标题的位置部分.不确定设置,但我确定.

You want to find the location part of the header. not sure the setting im sure though.

编辑 3..或 4?是的,我明白发生了什么.您实际上想跟踪位置 url,然后在不下载文件的情况下回显有效 url.试试.

EDIT 3..or 4? Yeah right, I see whats happening. You actually want to follow the location url then echo the effective url without downloading file. try.

$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

当我运行这个时,我的输出是

When I run this my output is

[username@localhost ~]$ php test.php
http://lq.mzc.in/data48-2/37202/Appy_Budday_(Videshi)-Santokh_Singh(www.Mzc.in).mp3

这篇关于将远程 url 上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆