Coders Den

Get Mime Type of file without reading file metas

What is MIME Type? MIME stands for Multipurpose Internet Mail Extension. It is the standardized way to identify the nature and format of a document. It is defined and standardized in IETF RFC 6838. The Internet Assigned Numbers Authority (IANA) is the official body who is responsible for keeping track of all official MIME types, and you can find the most up-to-date and complete list at the Media Types page. Syntax of MIME Type type/subtype MIME type string is a combination of 2 strings that are Type & SubType separated by / symbol. There should not be any space between Type & SubType. Type is a category of multiple MIME types of common groups that are closely related to each other. SubType …

Read More

What are traits in PHP OOPS / Laravel ?

What is Traits? Traits are a mechanism that allows you to create reusable code in languages like PHP where multiple inheritance is not supported. A Trait cannot be instantiated on its own. In layman language,  traits are simply a group of methods that you want to include within another class. A Trait is like an abstract class that cannot be instantiated by itself. The trait is created to reduce the limitations of single inheritance in PHP by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. Example of Trait:

To use Trait within the other classes use use keyword with Trait name. i.e. user AbcClass;

Now if you create new objects …

Read More
Ubuntu iMagick

How to install iMagick for PHP 7.x in Ubuntu 14 and above

Imagick is the most famous & best PHP extension for image manipulations. You can create and modify images using the iMagick (ImageMagick) library. It’s known on other platforms due to its difficult installation process, however in Ubuntu, installing it is pretty easy thanks to the php-imagick package that installs ImageMagick and Imagick as well. If you want to install Imagick on your server in just a couple of minutes, follow these steps: Install PHP Imagick To use Imagick with PHP 7.x in your server, you can easily achieve it with the following command:

The source of the package is the original package of Imagick from pecl. The installation takes about 1-2 minutes. You just need to press ‘Y’ each …

Read More
set default php version

Set Default PHP Version in Ubuntu

You can set the default PHP version to be used on the system with the “update-alternatives” command, after setting it, check the PHP version to confirm whether desired PHP version is set as default or not.

Restart apache server

Check PHP version

Output of above command is Check PHP configuration file You can find your PHP configuration file, by running the command below.

Output of above command is Now, check the current PHP version with phpinfo. Sometime you may see phpinfo() function shows older/different PHP version and SSH command line shows newer/different PHP version. To over come with this issue you need to disable unwanted PHP version and enable desired installed PHP version. Disable PHP5 module …

Read More
Could not instantiate mail function - PHPMailer

Could not instantiate mail function – PHPMailer

If you are using PHPMailer just to send mail and you are not using isSMTP() then PHPMailer uses PHP’s mail() function. If your server does not support PHP mail() function. Then PHPMailer will throw Fetal Error. And this error is well defined on PHPMailer Github Documentation. PHPMailer without isSMTP() not working? This means that your PHP installation is not configured to call the mail() function correctly (e.g. sendmail_path is not set correctly in your php.ini), or you have no local mail server installed and configured. To fix this you need to do one or more of these things: Install a local mail server (e.g. postfix). Ensure that your sendmail_path points at the sendmail binary (usually /usr/sbin/sendmail) in your php.ini. Note …

Read More
php mail

Enable PHP mail() function on Ubuntu Server

PHP mail() function does not come with default installation of AWS EC Ubuntu Server. You need to install “sendmail” package to enable PHP mail() function on Ubuntu Server. This is a short guide covers how to enable the mail() function in PHP on Ubuntu Server. Here are the steps I took to get “mail()” working on my Ubuntu Server. Step 1: Install sendmail The first thing you need to do is install the “sendmail” package. Do this by running the following command in your terminal:

Sendmail might already be installed, but just in case, enter above into your terminal: Step 2: Configure sendmail package After successful installation of “sendmail” package, you need to configure it. You can do that by running the …

Read More
WordPress-Mail.jpg

How to send mail in wordpress?

You can send emails from WordPress function that is:

wp_mail Parameters $to (string|array) (Required) Array or comma-separated list of email addresses to send message. $subject (string) (Required) Email subject $message (string) (Required) Message contents $headers (string|array) (Optional) Additional headers. Default value: ” $attachments (string|array) (Optional) Files to attach. Default value: array()

     

Read More
php mail

How to send mail in PHP (predefined mail function)

PHP mail() is the built-in PHP function that is used to send emails from directly PHP scripts. Using PHP’s mail() function it’s possible. Remember mail function will not work on the Local server. mail function Syntax:

Sending plain text mail

Sending HTML mail

To send single mail to multiple email address you can either choose CC option or you can add comma separated email address in “To” parameter.

Read More

How to upload local file on the FTP/SFTP server – PHP

Sometime you may need to upload files from the local server to live server or one server to another server without FTP/SFTP clients. Here is a sample code that will allow you to transfer your file/files from one server to another. File transfer on FTP/SFTP server FTP – File Transfer Protocol SFTP – Secure File Transfer Protocol Source: My GitHub Account View the code on Gist.  

Read More

How to delete all files of a directory in PHP

Here is a PHP small snippet that will delete all/specific files from a directory.

 

Read More