ReadFile in Base64 Nodejs

Latest and greatest way to do this: Node supports file and buffer operations with the base64 encoding: const fs = require(‘fs’); const contents = fs.readFileSync(‘/path/to/file.jpg’, {encoding: ‘base64’}); Or using the new promises API: const fs = require(‘fs’).promises; const contents = await fs.readFile(‘/path/to/file.jpg’, {encoding: ‘base64’});

PHP Streaming MP3

Here’s what did the trick. $dir = dirname($_SERVER[‘DOCUMENT_ROOT’]).”/protected_content”; $filename = $_GET[‘file’]; $file = $dir.”https://stackoverflow.com/”.$filename; $extension = “mp3”; $mime_type = “audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3”; if(file_exists($file)){ header(‘Content-type: {$mime_type}’); header(‘Content-length: ‘ . filesize($file)); header(‘Content-Disposition: filename=”‘ . $filename); header(‘X-Pad: avoid browser bug’); header(‘Cache-Control: no-cache’); readfile($file); }else{ header(“HTTP/1.0 404 Not Found”); }

C++ Read file line by line then split each line using the delimiter

Try: Note: if chr can contain more than 1 character then use a string to represent it. std::ifstream file(“plop”); std::string line; while(std::getline(file, line)) { std::stringstream linestream(line); std::string data; int val1; int val2; // If you have truly tab delimited data use getline() with third parameter. // If your data is just white space separated data … Read more

Reading mp4 files with PHP

You need to implement the skipping functionality yourself in PHP. This is a code snippet that will do that. <?php $path=”file.mp4″; $size=filesize($path); $fm=@fopen($path,’rb’); if(!$fm) { // You can also redirect here header (“HTTP/1.0 404 Not Found”); die(); } $begin=0; $end=$size; if(isset($_SERVER[‘HTTP_RANGE’])) { if(preg_match(‘/bytes=\h*(\d+)-(\d*)[\D.*]?/i’, $_SERVER[‘HTTP_RANGE’], $matches)) { $begin=intval($matches[0]); if(!empty($matches[1])) { $end=intval($matches[1]); } } } if($begin>0||$end<$size) header(‘HTTP/1.0 … Read more

open read and close a file in 1 line of code

You don’t really have to close it – Python will do it automatically either during garbage collection or at program exit. But as @delnan noted, it’s better practice to explicitly close it for various reasons. So, what you can do to keep it short, simple and explicit: with open(‘pagehead.section.htm’, ‘r’) as f: output = f.read() … Read more

The fastest way to read input in Python

numpy has the functions loadtxt and genfromtxt, but neither is particularly fast. One of the fastest text readers available in a widely distributed library is the read_csv function in pandas (http://pandas.pydata.org/). On my computer, reading 5 million lines containing two integers per line takes about 46 seconds with numpy.loadtxt, 26 seconds with numpy.genfromtxt, and a … Read more