Upload remote URL to server

I use the following codes to upload deleted files to my server. It works great when a direct download link is given, but recently I noticed that several sites provide mysql links as a download link, and when we click on this link, the files start to be downloaded to my computer. But even in the html source of this page, it does not show a 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 fine for all links where the download link is direct, for example, if I give http://priceinindia.org/muzicpc/48.php?id=415508 , it will download the music file, but the file name will be 48.php? id = 415508, but the actual mp3 file will be saved in
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, it will be the name Appy_Budday_ (Videshi) -Santokh_Singh (www.Mzc.in) .mp3

So, I want to get the actual download url.

0
source share
2 answers

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

+1 for Robbie using CURL.

If you run (from the 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 that the location header 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 part of the header location. not sure if the installation is really true.

EDIT 3..or 4? Yes, I see what is happening. You really want to follow location URLs and then echo the effective URL without downloading the file. try it.

 $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,

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

To do this, you should use the Curl library. http://php.net/manual/en/book.curl.php

An example of how to use curl is provided in the manual (at this link) before closing the connections, call curl_getinfo (http://php.net/manual/en/function.curl-getinfo.php) and, in particular, get CURLINFO_EFFECTIVE_URL 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 parameters. Also in the manual)

+1
source

Source: https://habr.com/ru/post/1438536/


All Articles