uploading remote url to server

2019-08-31 05:49发布

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.

Here is my code:

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

?>

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

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.

标签: php url
2条回答
何必那么认真
2楼-- · 2019-08-31 06:05

You should use Curl library for this. http://php.net/manual/en/book.curl.php

An example of how to use curl is provided in tha manual (on that link) befo before you close the connections, call curl_getinfo (http://php.net/manual/en/function.curl-getinfo.php) and specifically get CURLINFO_EFFECTIVE_URL which is what you want.

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

(You can also use curl to write directly to a file - use the CURLOPT_FILE options. Also in the manual)

查看更多
劳资没心,怎么记你
3楼-- · 2019-08-31 06:13

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.

+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.

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.

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
查看更多
登录 后发表回答