PHP commonly used code snippets

If you have max code execution limit error you can try resolve with following code  ini_set("max_execution_time", "1800");  ini_set('memory_limit','1048M'); if you want to display all hidden php errors  ini_set('display_errors', 1);

PHP larger random value generation

When customizing image names or file names during uploads, it is necessary to generate larger unique names. It can be done with following php function.  function generateRandomString($len = 80){  $base = 'abcdefghjkmnpqrstwxyz123456789';  $max = strlen($base)-1;  $string = '';  while(strlen($string)&nbsp...

Using Aws s3 api with php for image uploading

Aws s3 is handy tool for storing and managing large image base for work. Following is the code skeleton for this purpose. require $_SERVER['DOCUMENT_ROOT'].'/assets/s3-upload/vendor/autoload.php'; // s3 image upload /////////////////// if(!empty($_FILES)){ $s3 = new Aws\S3\S3Client([ 'region' => $this->config->item('s3_region'), 'version' => 'latest', 'credentials...

Exporting php array to csv

public function export($results){ $file_name = 'fusion_search_on_'.date('Ymd').'.csv'; header("Content-Description: File Transfer"); header("Content-Type: application/csv;"); header("Content-Disposition: attachment; filename=$file_name"); // file creation $file = fopen('php://output', 'w'); $header = ['Incoming Date','WWNDT Job #', 'Operator', 'Division', 'Job Number',...

Cron Job curl with url

<p>Sometimes it is necessary to automate tasks with cron job in a project. In order to do that cpanel gives some default syntax. Along with that you need to curl the url to setup the cron job correctly.</p><p>Following is one example way of doing it.</p><p>curl --silent https://some-url.com & >/dev/null<br /><br />Above --silent is used to a...

How to list youtube videos in your site using Youtube api

This can be done in following way.  FIrstly, in the coding you need to setup and put youtube api key. Secondly, use the channel id from where you want to bring the youtube videos. $API_key = 'youtube-api-key'; $channelID = 'channel-id'; $maxResults = 10; $videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId...