0

I have a TXT files with URLs to images on every new file. I am reading the files and fetching each new URL and fecthcing the image and storing it locally as below.

$filename = 'images.txt';
displayTXTList($filename);

function displayTXTList($fileName) {
    if(file_exists($fileName)) {
        $file = fopen($fileName, 'r');
        while (!feof($file)) {
            $url = fgets($file);
            $image = basename($url);
            downloadImages($image);
            downloadImages(str_replace(".jpg", 'l.jpg ', $image));
            downloadImages(str_replace(".jpg", 's.jpg ', $image));
        }
        fclose($file);
    }
}

function downloadImages($image) {
    $ch = curl_init('http://i.imgur.com/'.$image);
    $fp = fopen('images/'.$image, 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    //file_put_contents('images/'.$i, file_get_contents('http://i.imgur.com/'.$i));
}

I am getting the following error

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\wamp\www\temp\nomad\index.php on line 45
Warning: fopen(images/BrRdRl.jpg ) [function.fopen]: failed to open stream: Invalid argument in C:\wamp\www\temp\nomad\index.php on line 40
Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in C:\wamp\www\temp\nomad\index.php on line 41

ÿØÿàJFIFHHÿÛC  % !###&)&")"#"ÿÛC """""""""""""""""""""""""""""""""""""""""""""""""""ÿÀ«€"ÿÄÿÄ?!1AQ"aq‘2¡#B±ÁÑRáð$3bñCr‚’4SÿÄÿÄ2!1AQ"2aq‘¡±#BÁð3RáCÿÚ?²'¿>Ô ïš`#984ðv<«ë,ùÚ7§¬¸##½ç1N¹¥HóvÜtë\¯Ó4cÀ"¨mÞ€$yƒÚ† 9Æß4ºö© ý)uõνÌ#?Û­&¢@ Ó°D= Ý€§Î9Ò >}éQŠn ø  áÉØšPN{T“¾9r¥8ØŸ­NÕƒ]¨d`TE‘èv¢‰NFã­Î6ö¥ÉúPÕ¶ÔiCd`1NÆ~{ÐÃo±ýiFG<Ð1ùÅ 9ÆûŠi<?lWr1µ'ÚšI ŽôÐyæ—##<©XØnFç­vq€yÒ}pÆsLuÍ&7?5Ù Ž´†hÙÜæ“=É®Ï*LŽx dc<ý©Å²6Ðk¿š€8;ÓµoËïC$ç}8óÇS@R@èq\IÎôÀ0ÛR俀Yߨ§ÁjÜíúRç<¶ ë ç;R‰7ÅV9ÓA'  ±íš]CN9÷¨ù Ò†ÿ†€HΡÈ×gÒQÖƒsŽUŰ»òmG<ë³±ýqAGù®-±É A¹ÎÞô Ÿ½FÔJ×d俇Øx4…Àçš@9È÷®1ÓæAΚd;ãô¦lAÚ¹ïHçÓ¾ç­4€AçΓP9èkƒmŠ`p;s]ƒA?4º¶ØRÇLý(ˆ<ö4¤`íýi¤“ÓjâO*Liá‚ämCÎÛ|Ó‹d`|Ó{)rÆ»ùr®Î:ïM=Å †<ö«ƒíšBÙÍ3rH;j<œdSIÞ°¿½!tÆiÝá¾q¸ö¦äަ“8;òÀoœäQ`qçîiÁˆ·ô¦©Á&’A#•‚ÐíC&w |ÒŽuÇ¥ìs…¦u=;×dcs¸®$ã•¿óIÈ×z.ìÉÒ26¦’š\Œc|Òg}¶¡Ç–Æ»V6Ü×mŒcíM úГô¤ÓŸWù¤mÀÏsK¬7ùÞ¢Àz¶ JâÀg~};Ó5ïŠB #®(õ?AñOÉÁ ŠføÈ;R“¿#Ö€ §#Ôp=Í(ÿÈíýi™Æ S•ÀaÈžÔ˜ÇJïϧÅ0Nû╘’y|šinc1Y‡!ŒôÍ*îO:fœößÞœ¸2}éúí9<Ž;S—™ÏÓÝ`œ ä;ïèAA3¹Ï:A’Ù¤ÜÆM*Œu¢Àpm/Œèºð§oµ‘åJ9îhuq³ñN×¾3õ4ÄmŸjPv4:§¯ë]«¹ëBÀÈ;S³“µHTRfûB0ÛŠ ¶ç4„çßÛ4!†ãÚ»9Þ„Îôºú$ò8®ÎßZlíž\©Äž¼¨<õÎÛÒjÛ–)®Æ“QƒƒEއ÷Çë\E46O1J ÿnÕÙ9ÜÓ É®ÕÒ‹€ã—Jì÷¡äô»=(0€ázWglf‡’;jí[ò¥bÄSÃ`mÓ¸ †Ûaµ.­ÆqEØPLíÊ»#VI SÁK«j:üŽB“9çLȰëΈyc¶F{mIÀþ´Üûüm]«plÑ`<žÙÇÅ'!Ó~ô€“Þ—¯?j,tvÄçµq¡¦’kºS±ÉÆõÀo)qšîœèû³¿jLŒò&š;θrçH«gé ß~Tƒc]«;v¦³¶Ç•'0w¡±qÒ”Cz~9gqîqK±ß•7'' }©w#aEŽ…ÎûóÒÍ7ÕœšP 9Ð!F:ã4…Žø®Ã:S‚“üÛÑc¦7;÷ùÙÆ:ûW2œP?—Ê8É#4Z cNØ¥ÛzBÃ#šì†æÊ1í@¨ãƒþ+Ž>ôá;‡=:k.’0A‹156Ûæ¸œàæš}ö¤$|üÑ`/3ê®þõÙä Í&AÎÍ;¹œã÷®ý=é7;•ÃnôÀâO\WoÓÚˆ¤$c oïEЇ †p0i¤íµ4“˯zâ=^Ô[¨ãÕý©2r~˜¡@nÝ)CY¨LæÔìåi àg#4»’psGc¶’4çn†—YÜv¦öÛzRçúP* œÎqÒ—P5ÀÆýiNO=ÿ¥¹<ö®o‘Ë­4d ñö§óå@ ƒœw¥Îã½.2» E÷¢ÀvsùFiN)9.)‡:Vúü×g'§À¦gmÆÔýCNˆPH"”`ò¦êa±¥ ¹ÇŒÿÁK“þô2pyNÎÙÇj@<rç]¨u;ô¦ô¥@ܨä9Ò :`b7ÛíJ+·åö¦À~{×tÞš.[õ¤ÈÏ÷¤…cóŠv£é™Î7¥9ý©Ø$.Iï÷§O\žÔÍ[îkµ„àÑc¦<žÃ«Î˜¸½+ ÕêÏޜͲ(%ð6ú×yƒÛäRØí ä¯F?jnO6Á¡k$mI«zh8ÓùüR lÙ¡ø® ’yS È ?JfsÖ¸A TVߘ¥vÄþaŸz'OÒ1€¤ŒŸÌ3]¤rð(Z,Ÿ½v®ô¾}IúWd`oÒ†Ûf»';uéEˆ&q¸'ï\IçèzŽw®,OZv€&­[Rî3VO?µ mñHAßû×g|mC-·=ë½8ï@è&tiŽ”=`‘©FÛvõg©ù®ÎÔñØ×gµõ ¶5ÙÛ4ÐG½q9Øb†ÇCĘaJ$# ŽÂ…žÄWdrÎÔ†QÇ3I¬ç™9çŠëϘ Q;Xž­ñša|ó8ïMÍvß4¬{«·Þ‘˜ïý)Δ¶&ŠŽ|‘JF¡éJnÿÙœò¢Æ(ôüu¤;{g ®b½²i¹j,j:wé\NÝé23½v¼õÚÓ¶1óK‘Ì …¶Þ“P'–(lçp6ù¦ë¨ZŽÜ f郿jÃàý)5óC‰öýisÊ€Xç¤;afÁ4ÍXÔqïLDà v3œoš`ÜçmùRõëj†!Ç=Æ)@ ïÊ“VvÇ;ô¨åèt;8î6®þmÁ'曯O1õ§gz{1ú³‚)Gq“ïMœàûRç¶>iA#–ô£9®É3ïEŽ„ÖqɉíOéìE'3]‘¾ÔZÙßÒ”¾ø#=3ÔGûR€q°¢ÐPýDíŠPG<}(dq½qcž´†[²­;^Np3ñAR@9æk³œp(PÄ¿Z][DýèE»b‚3°"€×µ8HA8'éBj@=èÞkéç±®ÎÃ¥c¹ÏA\6ݶ t9ëµpôäéÈ¡jßt¤,r7ø¦‰ ùßa]¨ç}èY$nMv­½ÅœŒŒRë厔-X “‘Ú”0ßa÷¤9#j\еgJvJ®3óEŽ‡ç¹®8è)‡¯>^Ôíð>Ô¬8‹«¿Z\íÓÞ˜W|šNGN)…Üœ‚>” r¦ƒØb¸9ß‘ïE…'}Îô¤úN(a;Š\㘠(PûSµwÚ™ØÒr眂tù¦ã~7Xf›«¶Â€ Û÷ÞŒo¶hi!Ú”1í@PMX¿ZM[íLÉaÊ—Ô~jAC‡.uÛçŠL]i2zšvGuÛ;S†Ý逜~o¥.I8&‹

When i shorten the list of images to 2-3. I see that only the last image is being downloaded.

0

2 Answers 2

2

You have a space in the file name:

downloadImages(str_replace(".jpg", 'l.jpg ', $image));
downloadImages(str_replace(".jpg", 's.jpg ', $image));
                                         ^ here

There shouldn't be a space after jpg

Sign up to request clarification or add additional context in comments.

1 Comment

i changed that still i have a problem. When i shorten the list of images to 2-3. I see that only the last image is being downloaded.
1
$fp = fopen('images/'.$image, 'wb');

This call is resulting in failure and resource handle is not being generated

2 Comments

how to solve it ? i can see that from the line that is pointing to this code
@HarshaMV Above that call, temporarily echo this echo 'i.imgur.com/'.$image; (with http) This will explain whats wrong in the url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.