{"id":1824,"date":"2008-11-10T16:57:37","date_gmt":"2008-11-10T16:57:37","guid":{"rendered":"http:\/\/dev001.dl\/yan\/wdp\/?p=8"},"modified":"2026-01-20T05:06:28","modified_gmt":"2026-01-20T05:06:28","slug":"file-access","status":"publish","type":"post","link":"http:\/\/www.lalife.net\/?p=1824","title":{"rendered":"PHP File access"},"content":{"rendered":"<p><strong>Read by lines<\/strong><\/p>\n<div style=\"background-color:#ffffe0\">\n<p>$hd = fopen(&#8220;$file&#8221;, &#8220;r&#8221;);<\/p>\n<p>if($hd){<\/p>\n<p>while(!feof($hd)){<\/p>\n<p>$line = fgets($hd);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>fclose($hd);<\/p><\/div>\n<p><strong>Read the whole file in<\/strong><\/p>\n<div style=\"background-color:#ffffe0\">$hd = fopen(&#8220;$file&#8221;, &#8220;r&#8221;);if($hd){<\/p>\n<p>$contents = fread($hd, filesize($file));<\/p>\n<p>}<\/p>\n<p>fclose($hd);<\/p><\/div>\n<p><strong>Read image in binary<\/strong><\/p>\n<div style=\"background-color:#ffffe0\">if(!($img = fopen(&#8220;$img_path&#8221;,&#8221;rb&#8221;))){<\/p>\n<p>echo\u00a0&#8220;couldn&#8217;t open $img_path&#8221;;<\/p>\n<p>die;<\/p>\n<p>}<\/p>\n<p>while (!feof($img)) {<\/p>\n<p>$contents .= fread($img, 1024);<\/p>\n<p>}<\/p>\n<p>fclose($img);<\/p>\n<p>###Header for image<br \/>\nheader(&#8220;Cache-Control: public&#8221;);<\/p>\n<p>if(eregi(&#8220;\\.[jpg|jpeg]&#8221;,$file)){<\/p>\n<p>header (&#8220;Content-type: image\/jpg&#8221;);<\/p>\n<p>header(&#8220;Content-Disposition: inline; filename=$file&#8221;);<\/p>\n<p>#header(&#8216;Content-Length: &#8216; . filesize($img));<\/p>\n<p>print $contents;<\/p>\n<p>}<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Read by lines $hd = fopen(&#8220;$file&#8221;, &#8220;r&#8221;); if($hd){ while(!feof($hd)){ $line = fgets($hd); } } fclose($hd); Read the whole file in $hd = fopen(&#8220;$file&#8221;, &#8220;r&#8221;);if($hd){ $contents = fread($hd, filesize($file)); } fclose($hd); Read image in binary if(!($img = fopen(&#8220;$img_path&#8221;,&#8221;rb&#8221;))){ echo\u00a0&#8220;couldn&#8217;t open $img_path&#8221;; &hellip; <a href=\"http:\/\/www.lalife.net\/?p=1824\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-1824","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/posts\/1824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lalife.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1824"}],"version-history":[{"count":1,"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/posts\/1824\/revisions"}],"predecessor-version":[{"id":1974,"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/posts\/1824\/revisions\/1974"}],"wp:attachment":[{"href":"http:\/\/www.lalife.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lalife.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1824"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lalife.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}