{"id":1635,"date":"2016-01-20T12:46:30","date_gmt":"2016-01-20T12:46:30","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1635"},"modified":"2025-07-09T08:45:01","modified_gmt":"2025-07-09T07:45:01","slug":"read-file","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/files\/read-file","title":{"rendered":"Read file"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Read a text file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  if (is_file('some_filename.json'))\n  {\n    $FileContent = file_get_contents('some_filename.json');\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read a binary file to an array<\/h4>\n\n\n\n<p>Your file may be byte based by PHP works to stop you operating at a raw byte level, so you have to workaround&nbsp;that a bit by using unpack<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $filename = \"myfile.bin\";\n  if (is_file($filename))\n  {\n    $handle = fopen($filename, \"rb\"); \n    $contents = fread($handle, filesize($filename)); \n    $byteArray = unpack(\"C*\",$contents); \t\t\t\t\/\/&lt;&lt;&lt;Warning the outputted array starts from &#91;1] not &#91;0] !!!!!! Bloody PHP...\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\/\/N.B. the C* designates unsigned char repeating, but actually unpack can create arrays that are not byte based no problem from teh file...<\/code><\/pre>\n\n\n\n<p>If your file is very big then get clever with fread etc and only read chunks at a time<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Read binary file 1 byte&nbsp;at a time<\/h4>\n\n\n\n<p>Using ord() &#8211; good for 1 byte at a time<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  fseek($handle, -100, SEEK_END );\n  for ($Count = 0; $Count &lt; 100; $Count++)\n  {\n    $OurByteValue = ord(fread($handle, 1));\n    \/\/echo \"&#91;\" . intval($OurByteValue) . \"]\";          \/\/Display integer\n    echo \"&#91;\" .  sprintf(\"%02X\", $OurByteValue) . \"]\";  \/\/Display hex\n  }\n<\/code><\/pre>\n\n\n\n<p>Using unpack() &#8211; better for arrays of bytes really, but works fine for 1 byte at a time too<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/Get and display the first 100 bytes of the file, reading 1 byte at a time\n  fseek($handle, 0, SEEK_SET );\n  for ($Count = 0; $Count &lt; 100; $Count++)\n  {\n    $FileByteArray = unpack(\"C*\", fread($handle, 1)); \t\t\/\/&lt;&lt;&lt;Warning the outputted array starts from &#91;1] not &#91;0] !!!!!! Bloody PHP...\n    \/\/echo \"&#91;\" . intval($FileByteArray&#91;1]) . \"]\";            \/\/Display integer\n    echo \"&#91;\" .  sprintf(\"%02X\", $FileByteArray&#91;1]) . \"]\";    \/\/Display hex\n  }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Read a text file Read a binary file to an array Your file may be byte based by PHP works to stop you operating at a raw byte level, so you have to workaround&nbsp;that a bit by using unpack If your file is very big then get clever with fread etc and only read chunks [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-1635","post","type-post","status-publish","format-standard","hentry","category-files"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=1635"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1635\/revisions"}],"predecessor-version":[{"id":5067,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1635\/revisions\/5067"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}