In short, how do I improve my code? I have a couple of files on Mediafire.com, link to shared folder for the purpose of this question. I would like to get a list of download-links and filenames so I can put them on my website for easy access. The problem is, mediafire doesn't make it easy!

I'd better mention that I own the files I'm working with. I have the mediafire-account the files are in. I could login to mediafire and copy each link, then copy the filename and paste it into a file. But where is the fun in that? And it would take forever to keep track of what files I deleted or added to my account. It's easier to do it this way.

The links I have to my files are in the wrong format so I needed to remake them in order to fit my purpose.

Wrong format:


Format I want them in:


Then, from the correct format I need to get the filename. After a lot of trial and error I finally got it working.

But there must be a better way of doing it? When I read the list from file and I have, lets say 50+ links it takes forever and I sometimes get Timeout errors.

I have a local server so I can pretty much do what I want on my end, but PHP is what I "know" (read: manages to find code online that I can rewrite to my needs).

I have tried DomDocument but it kept giving me errors (the manual was no use since it was there I got the code I tried to use). I gave curl a try and it gave me almost what I was looking for but since PHP is what I used to know some 10 years ago, that is what I went back to.

If someone wants to answer this then could you do it in PHP since I can "read" that and understand what is going on.

ini_set('auto_detect_line_endings', true);

//obviusly this is where I get the incorrect links
//and i have several files with links... thats why i did it this way.
foreach (glob("./mediafire*.txt") as $files) {  
    if($files == '.' || $files == '..') continue;

if (($file = fopen("$files", "r")) !== FALSE) {

    // This works and I don't dare to change it ( and this is what one row in "mediafire*.txt 
    // looks like http://www.mediafire.com/download.php?kt9z2284k2sg8ay )
    // I get the part after ? to $f1
    while (($line = fgetcsv($file, 500, "\t")) !== false) { 
        $f1 = $line[0];
        $f1 = explode('?',$f1);
        $f1 = trim(array_pop($f1));         

    //Make the link look like I want it and can use to get the filename-information.
    $var_mediafire = "http://www.mediafire.com/file/";
    $var_full_link = $var_mediafire . $f1; 

    //Get the page with the filename information. 
    //From this point is where I think there must be a better/faster solution.
    $dllink = file_get_contents("$var_full_link", true); 

    $mystring = $dllink;
    $findthis = '<div class="fileName">'; //This is what I'm looking for from the download-page
    $pos = strpos($mystring, $findthis);

    if ($pos === false) {
        echo "The string was not found ";
    } else {
            $foundit = substr(strstr($mystring,$findthis),strlen($findthis),250); //I just take the next 250 chars
            $nextpos = strpos($foundit, '<');

            $myfilename = substr($foundit,0,$nextpos);

    //what should we write to file?
    $writethis = $var_full_link .';'. $myfilename ."\n";
//          echo $writethis;
            $myfile = 'mywonderfullinksandfilenames.txt';
            file_put_contents($myfile, $writethis, FILE_APPEND); 
// I really would like to get the file size as well, it is in the "$foundit" variable 
//but it takes to long to execute already.

  • \$\begingroup\$ Welcome to Code Review. I'll hope a PHP wizard will have a look at your code. Keep in mind that we review your code. A review can provide another solution, but it doesn't need to. \$\endgroup\$ – Zeta Apr 16 '18 at 4:40
  • \$\begingroup\$ Thank you for the edit Jamal! Much more readable now :) Zeta, yes I'm aware of that, hopefully I don't get "get another filehost" or "Write it in C++" but any serversided code would be fine really! :) \$\endgroup\$ – Gmail2010 Apr 16 '18 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.