{"id":1445,"date":"2022-08-30T15:16:35","date_gmt":"2022-08-30T15:16:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/19\/php-detecting-fopen-failures-when-downloading-images-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:35","modified_gmt":"2022-08-30T15:16:35","slug":"php-detecting-fopen-failures-when-downloading-images-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/php-detecting-fopen-failures-when-downloading-images-collection-of-common-programming-errors\/","title":{"rendered":"PHP: Detecting fopen() failures when downloading images-Collection of common programming errors"},"content":{"rendered":"<p>I have a card game (screenshot below) in which I display player avatars.<\/p>\n<p>For the avatars I&#8217;ve written a short proxy.php script, which would take an image URL passed to it as <strong>?img=<\/strong> parameter, download it and save under <strong>\/var\/www\/cached_avatars\/md5_of_that_url<\/strong> at my CentOS 5 machine. Next time the script is called with the same URL, it will find that image in the dir and serve it directly to STDOUT.<\/p>\n<p>This works mostly well, but for some avatars the initial download fails (I suppose it times out) and you don&#8217;t see the lower part of the player picture:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/5JdiP.png\" \/><\/p>\n<p>I&#8217;d like to detect this image download failure and delete the cached partial file, so that it is re-downloaded on the next proxy.php call.<\/p>\n<p>I&#8217;ve tried detecting STREAM_NOTIFY_FAILURE or STREAM_NOTIFY_COMPLETED events in my callback, but they are not fired. The only events I see are: STREAM_NOTIFY_CONNECT, STREAM_NOTIFY_MIME_TYPE_IS, STREAM_NOTIFY_FILE_SIZE_IS, STREAM_NOTIFY_REDIRECTED, STREAM_NOTIFY_PROGRESS:<\/p>\n<pre><code>Nov  3 18:48:27 httpd: 2  0\nNov  3 18:48:27 httpd: 4 image\/jpeg 0\nNov  3 18:48:27 httpd: 5 Content-Length: 45842 0\nNov  3 18:48:27 httpd: 7  0\nNov  3 18:48:27 last message repeated 16 times\nNov  3 18:48:39 httpd: 2  0\nNov  3 18:48:40 httpd: 4 image\/jpeg 0\nNov  3 18:48:40 httpd: 5 Content-Length: 124537 0\nNov  3 18:48:40 httpd: 7  0\n<\/code><\/pre>\n<p>And my even bigger problem is, that I can&#8217;t pass variables like $img or $cached into the callback or I can&#8217;t set a $length variable in the callback on a STREAM_NOTIFY_FILE_SIZE_IS event and then compare it with filesize($cached) in the main script (I could detect the mismatch and delete the file):<\/p>\n<pre><code>Nov  3 18:50:17 httpd: PHP Notice:  Undefined variable: length in \/var\/www\/html\/proxy.php on line 58\nNov  3 18:50:17 httpd: length=\n<\/code><\/pre>\n<p>Does anybody have a solution for my problem?<\/p>\n<p>I&#8217;ve looked at the PHP curl library, but don&#8217;t see how could it help me here.<\/p>\n<p>Below is my script, I&#8217;ve omitted the URL sanity checks for brevity:<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-19 13:16:45. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a card game (screenshot below) in which I display player avatars. For the avatars I&#8217;ve written a short proxy.php script, which would take an image URL passed to it as ?img= parameter, download it and save under \/var\/www\/cached_avatars\/md5_of_that_url at my CentOS 5 machine. Next time the script is called with the same URL, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1445","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1445"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1445\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}