PHP PortPing script – check host connectivity

Here is a script in PHP I wrote to check if a remote host and port are open for you to connect. Do not mistake this as an equivalent to ping command because ping uses ICMP and I used TCP using PHP’s famous fsockopen() function, and a small piece of AJAX.


You can get the latest source code from my GitHub code share page –


function ping($host, $port) {
         $startTime = time();
         if ($fs = fsockopen($host, $port, $errCode, $errStr, 1)) {
             $timeTaken = (time()-$startTime)/1000;
         echo "Reply from $host:$port time={$timeTaken}ms";
         } else {   
             echo "Request timed out.";
         echo "<br>";


var count = 0;
             var intervalID = setInterval(function PortPing() {
                 var xhttp = new XMLHttpRequest();
                 xhttp.onreadystatechange = function() {
                     if (this.readyState == 4 && this.status == 200) {
                         document.getElementById("console").innerHTML += this.responseText;
       "GET", "PortPing.php?source=self&host=<?=$host?>&port=<?=$port?>", true);
                 if (++count > <?=$t-1?>) {

Ajax file uploader using jQuery

Here is a sample code to upload multiple files using ajax, and without using the traditional <form> tags. Find the full source code in GitHub – .

    <input type="button" value="Choose file" id="fup" />

    <script src=""></script>    

            $(function() {

                // Show file upload dialog on click of button

                    //Create File component
                    var fu = document.createElement("input");
                    fu.type = "file";

                    // Trigger file selection event
                        var fd = new FormData();
                        $.each($(fu)[0].files, function(i, file){
                            fd.append('file[]', file);

                        // Send fd to server
                            type: 'POST',
                            cache: false,
                            contentType: false,
                            processData: false,
                            success: function(d) {

                    // To invoke the file selection dialog box


Sample server code in PHP for the reference:

foreach ($_FILES['file']['tmp_name'] as $k => $v) {