Category Archives: Uncategorized

Force Download of Remote File with PHP

Force the download of a remote file with PHP: $file_name = ‘crowd-cheering.mp3’; $file_url = ‘http://www.sample-videos.com/audio/mp3/’ . $file_name; header(‘Content-Type: application/octet-stream’); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"".$file_name."\""); readfile($file_url); exit;$file_name = ‘crowd-cheering.mp3’; $file_url = ‘http://www.sample-videos.com/audio/mp3/’ . $file_name; header(‘Content-Type: application/octet-stream’); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"".$file_name."\""); readfile($file_url); exit; Note that ini.allow-url-fopen must be enabled. Alternatively, download the file to disk and […]

Upload a File via POST with cURL and PHP 7

Example of sending or uploading a file via POST with cURL and PHP 7: File content of php-send-file-curl.php: <?php if ( isset($_POST[‘submitfile’]) ) { // Make sure there are no upload errors if ($_FILES[‘upfile’][‘error’] > 0) { die("Error uploading file…"); }   // Prepare the cURL file to upload, including file name and MIME type […]

Youtube embed iframe not working on mobile devices

Some days ago I noticed that Youtube iframe embed code was not working on mobile devices, instead it showed a blank space on mobile. I used the iframe code gathered from the Youtube -> Share -> Embed window. After some testings, I found the correct Youtube embed iframe code that works on iPhone, iPad and […]

Array as parameters in a PHP function

Sample PHP function that uses an array to handle arguments: function testFunction($args) { // $args contains the arguments of the function print_r($args); // Validate and assign params to variables $name = $args[‘name’] ? $args[‘name’] : "None"; $file = $args[‘file’] ? $args[‘file’] : "None"; $title = $args[‘title’] ? $args[‘title’] : "None"; }function testFunction($args) { // $args […]

Change Bootstrap Table Border Color

Change default border color of table-bordered class: .table-bordered { border: 1px solid #ecf0f1 !important; } .table-bordered > thead > tr > th, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > tbody > tr > td, .table-bordered > tfoot > tr […]

Node.js Execute a Unix Command

Use child_process.exec to execute a comand and fetch its output: var exec = require(‘child_process’).exec; var cmd = ‘id’;   exec(cmd, function(error, stdout, stderr) { console.log("Output: "+stdout); });var exec = require(‘child_process’).exec; var cmd = ‘id’; exec(cmd, function(error, stdout, stderr) { console.log("Output: "+stdout); }); Or you can use execFile() method: var execFile = require(‘child_process’).execFile;   execFile("/bin/ls", [‘-la’], […]

Node.js Cluster Module

Example Node.js cluster module usage: var cluster = require(‘cluster’); var http = require(‘http’); var numCPUs = require(‘os’).cpus().length;   if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); }   cluster.on(‘exit’, function(worker, code, signal) { console.log(‘worker ‘ + worker.process.pid + ‘ died’); }); } else { // Workers […]

Basic usage of jQuery $.ajax

Example Form HTML code: <form id="foo"> <label for="bar">A bar</label> <input id="bar" name="bar" type="text" value="" />   <input type="submit" value="Send" /> </form><form id="foo"> <label for="bar">A bar</label> <input id="bar" name="bar" type="text" value="" /> <input type="submit" value="Send" /> </form> Javascript code: // Variable to hold request var request;   // Bind to the submit event of our form […]

Best PHP HTML\XML Parser Class

This is the fastest and simplest HTML parser class for PHP: https://github.com/Imangazaliev/DiDOM Here is an example usage: use DiDom\Document;   $document = new Document(‘http://www.news.com/’, true);   $posts = $document->find(‘.post’);   foreach($posts as $post) { echo $post->text(), "\n"; }use DiDom\Document; $document = new Document(‘http://www.news.com/’, true); $posts = $document->find(‘.post’); foreach($posts as $post) { echo $post->text(), "\n"; } […]

Regex Remove Blank Lines from Text in PHP

This regex will remove (preg_replace) all empty new lines from a text: $str = preg_replace(‘/^\n+|^[\t\s]*\n+/m’, ”, $str);$str = preg_replace(‘/^\n+|^[\t\s]*\n+/m’, ”, $str);