Getting a filename from Mediafire

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












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:



http://www.mediafire.com/download.php?kt9z2284k2sg8ay


Format I want them in:



http://www.mediafire.com/file/kt9z2284k2sg8ay


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.



<?php
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) $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.

?>






share|improve this question





















  • 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.
    – Zeta
    Apr 16 at 4:40










  • 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! :)
    – Gmail2010
    Apr 16 at 11:17
















up vote
1
down vote

favorite












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:



http://www.mediafire.com/download.php?kt9z2284k2sg8ay


Format I want them in:



http://www.mediafire.com/file/kt9z2284k2sg8ay


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.



<?php
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) $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.

?>






share|improve this question





















  • 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.
    – Zeta
    Apr 16 at 4:40










  • 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! :)
    – Gmail2010
    Apr 16 at 11:17












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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:



http://www.mediafire.com/download.php?kt9z2284k2sg8ay


Format I want them in:



http://www.mediafire.com/file/kt9z2284k2sg8ay


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.



<?php
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) $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.

?>






share|improve this question













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:



http://www.mediafire.com/download.php?kt9z2284k2sg8ay


Format I want them in:



http://www.mediafire.com/file/kt9z2284k2sg8ay


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.



<?php
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) $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.

?>








share|improve this question












share|improve this question




share|improve this question








edited Apr 16 at 2:54









Jamal♦

30.1k11114225




30.1k11114225









asked Apr 16 at 2:23









Gmail2010

91




91











  • 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.
    – Zeta
    Apr 16 at 4:40










  • 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! :)
    – Gmail2010
    Apr 16 at 11:17
















  • 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.
    – Zeta
    Apr 16 at 4:40










  • 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! :)
    – Gmail2010
    Apr 16 at 11:17















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.
– Zeta
Apr 16 at 4:40




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.
– Zeta
Apr 16 at 4:40












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! :)
– Gmail2010
Apr 16 at 11:17




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! :)
– Gmail2010
Apr 16 at 11:17















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192157%2fgetting-a-filename-from-mediafire%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192157%2fgetting-a-filename-from-mediafire%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?