Programming Tips & Insights

PHP: Count Lines of a File and Get the Number of Lines in a Text File

As we know, lines are separated from each other by line breaks indicated by “\n” in PHP, therefore, one method would be to explode() the entire file content string by “\n” into an array and then count() the number of the array items to come to the number of lines of that file. However, there’s …

PHP: Count Lines of a File and Get the Number of Lines in a Text File Read More »

PHP: Reading a File into a String or Reading the File into an Array

Aside from the common approach in reading a file with php fopen() function which is a little annoying in that it requires extra steps before actually reading the contents of the file. PHP has other file reading functions to enable you instantly get what you want and read the contents of the file without the …

PHP: Reading a File into a String or Reading the File into an Array Read More »

PHP: Make or Create A Directory

The function in PHP that’s used to create a directory is just the same with that in Linux bash shell: mkdir. First make sure the current working directory is where you want the new directory to be, then run this line of PHP: mkdir(‘new_dir’); // creates a new directory named new_dir under the current directory

PHP: Delete File / Erase File / Remove File

As long as the owner of the php script has the necessary privilege to delete a file, the php script has the ability to delete it. To delete a file with PHP, use the unlink() function: unlink(‘tmpfile.txt’) or die(‘Unable to delete the file’); It returns true if the deletion is successful and false otherwise.

PHP: Getting Directory Path and Filename from A Full Path or URL

It’s common tasks to parse and deal with file paths or URL in PHP. And most common must be to identify and distinguish the directory path or file name from the file path or URL. For instance, the full path to a text file is: ‘/home/john/lanning.com/somefile.txt‘ and you want get the directory part as well …

PHP: Getting Directory Path and Filename from A Full Path or URL Read More »

Linux: The differences between file times: atime (accessed time), ctime (changed time) and mtime (modified time).

Unless you have explicitly opted out with a noatime option mounting your Linux file system, there are generally 3 types of time on each and every file of Linux: atime or access time, ctime or change time, and mtime or modification time. These are the differences between the 3 file system times: atime — access …

Linux: The differences between file times: atime (accessed time), ctime (changed time) and mtime (modified time). Read More »

PHP: Getting Last Changed Time of a File – File ctime

File ctime may be a little misleading in nature as many would think it as the creation time of the file, but actually it’s the meta data such as the ownership or permissions change time of the file in addition to content modification. To get the ctime of a file in PHP: $last = filectime(‘anyfile’); …

PHP: Getting Last Changed Time of a File – File ctime Read More »

PHP: Read from Keyboard – Get User Input from Keyboard Console by Typing

As I said how to parse php program command line arguments in the previous post, reading from user keyboard or console is also a considerable part of making interactive command line programs. How to let user type in something and make the php script read from the keyboard so that it can identify the user …

PHP: Read from Keyboard – Get User Input from Keyboard Console by Typing Read More »

Dealing with PHP command line programs / scripts run on shell and the arguments

PHP is a lot more than just HTML rendering and outputting. It can also be run from shell command line like any legacy command line programs or tools we had in DOS and Linux shell. One of the major problem is to deal with arguments passed to such program and how to identify them inside …

Dealing with PHP command line programs / scripts run on shell and the arguments Read More »

The Secure Way to Store Passwords with PHP

Passwords are indisputably one of the most sensitive data types to deal with for web developers, especially back end developers who are in charge of PHP and MySQL. Storing passwords is not tricky considering you are just required to do a one-way conversion of them: $password = ‘ilovjenny84’; $salt = ‘SHAKY SHARKY 333’; // whatever …

The Secure Way to Store Passwords with PHP Read More »

jQuery: Protecting or hiding variables / functions in a specific local scope

As everyone who’s well trained in software engineering would agree, that one of the principle of writing predictable code is to separate and hide something within only the scope that it’s needed. Controlled access to and from the outside world brings predictability and accountability of your code thus better debugging. In jQuery, you need constantly …

jQuery: Protecting or hiding variables / functions in a specific local scope Read More »

jQuery: Get the text and value of the selected option of HTML select element

For instance, there’s a HTML select element identified by the ID #title, and after the user has selected one of the options, you want to get the text and value of the selected option. <select id="title"> <option value="1">Mr</option> <option value="2">Mrs</option> <option value="3">Miss</option> </select> Now that the user has selected the 2nd option: Mrs. To get …

jQuery: Get the text and value of the selected option of HTML select element Read More »

Scroll to Top