Skip to content

Instantly share code, notes, and snippets.

View nagiyevelchin's full-sized avatar
🌴
On vacation

Elchin Nagiyev nagiyevelchin

🌴
On vacation
View GitHub Profile
@nagiyevelchin
nagiyevelchin / unzipper.php
Last active October 7, 2023 11:31
This PHP code is designed to handle zip file extraction - unzip based on a 'zip' parameter provided through a GET request.
<?php
// Check if the 'zip' parameter is empty in the GET request.
if(empty($_GET['zip'])){
// If 'zip' parameter is empty, terminate the script and display 'No zip!' message.
die('No zip!');
}
// Create a new instance of the ZipArchive class.
$zip = new ZipArchive;
// Try to open the zip file specified in the 'zip' parameter.
@nagiyevelchin
nagiyevelchin / getYoutubeVideoIdFromUrl.php
Last active October 7, 2023 11:28
This code defines a PHP function getYoutubeVideoIdFromUrl that takes a URL as input and attempts to extract a YouTube video ID from it using a regular expression. The function returns the extracted video ID if found or false if no match is found in the input URL. The regular expression is designed to handle various formats of YouTube video URLs.
<?php
/**
* Function to extract a YouTube video ID from a given URL.
*
* @param string $url The input URL that may contain a YouTube video link.
*
* @return string|false If a YouTube video ID is found, it is returned; otherwise, false is returned.
*/
function getYoutubeVideoIdFromUrl($url) {
// Regular expression to match YouTube video URLs
@nagiyevelchin
nagiyevelchin / formatBytes.php
Last active October 7, 2023 11:25
This PHP function takes a number of bytes as input and converts it into a human-readable format with units like KB, MB, GB, or TB, depending on the magnitude of the input. The precision parameter allows you to control the number of decimal places in the formatted result. The function is well-commented to explain each step of the process.
<?php
/**
* Format a given number of bytes into a human-readable string with specified precision.
*
* @param int|float $bytes The number of bytes to format.
* @param int $precision The number of decimal places for precision (default is 2).
*
* @return string The formatted string representing the bytes with appropriate units (e.g., KB, MB, GB).
*/
function formatBytes($bytes, $precision = 2) {
@nagiyevelchin
nagiyevelchin / copyr.php
Last active February 12, 2022 15:50
Copy a file, or recursively copy a folder and its contents in PHP
<?php
/**
* Copy a file, or recursively copy a folder and its contents
*
* @author Aidan Lister <[email protected]>
* @version 1.0.1
* @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
* @param string $source Source path
* @param string $dest Destination path
* @return bool Returns TRUE on success, FALSE on failure
@nagiyevelchin
nagiyevelchin / humanlyFileSize.php
Last active October 7, 2023 11:19
This PHP code defines a function called humanlyFileSize that is used to convert a file size in bytes into a human-readable format with size suffixes (e.g., KB, MB, GB).
<?php
// Define a function named humanlyFileSize that takes two parameters:
// $bytes - the file size in bytes
// $decimals (optional) - the number of decimal places to include in the result (default is 2)
function humanlyFileSize($bytes, $decimals = 2) {
// Define a string $sz containing the size suffixes for bytes (B), kilobytes (K), megabytes (M),
// gigabytes (G), terabytes (T), and petabytes (P).
$sz = 'BKMGTP';
// Calculate the appropriate size suffix based on the file size in bytes.
@nagiyevelchin
nagiyevelchin / checkVariableType.php
Last active February 12, 2022 15:52
Finds whether a variable is a needed type in PHP
<?php
/**
* Finds whether a variable is a unsigned float or a numeric string.
* Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
* @param string The variable being evaluated.
* @link http://php.net/manual/en/function.is-numeric.php#47491
* @return 1|emptystring It returns 1 if okay and returns nothing "" if it's bad number formating
* @since 11/6/14 11:15 AM
*/
function isUnsignedFloat($val) {
@nagiyevelchin
nagiyevelchin / httpCurlPost.php
Last active October 7, 2023 11:15
This PHP function is designed to send an HTTP POST request to a specified URL with given parameters, allowing you to customize headers and the referer. It uses the cURL library to handle the HTTP request and provides flexibility in customizing the request's headers and parameters. The function returns the response received from the POST request.
<?php
/**
* This function performs an HTTP POST request to a specified URL with given parameters.
*
* @param string $url The URL to which the POST request is sent.
* @param array $params An associative array containing the POST parameters as key-value pairs.
* @param string $referer The referer header for the request.
*
* @return mixed The response from the HTTP POST request.
*/
@nagiyevelchin
nagiyevelchin / detectMobileBrowsers.php
Last active February 12, 2022 15:54
Detect Mobile Browsers in PHP
<?php
/**
* Detect Mobile Browsers
* @link http://detectmobilebrowsers.com/
* @return boolean
*/
static function idMobileBrowser() {
$useragent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',
$useragent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|
@nagiyevelchin
nagiyevelchin / clientIPAddress.php
Last active February 12, 2022 15:54
Get clients IP address in PHP
<?php
/**
* Retuns client's IP address
* @return string
* @since 6/16/11 12:11 PM
*/
function getClientIPAddress() {
if (getenv("HTTP_CLIENT_IP")) {
$ip = getenv("HTTP_CLIENT_IP");
} else {
@nagiyevelchin
nagiyevelchin / IPToLong.php
Last active February 12, 2022 15:55
Convert IP address to longint in PHP
<?php
/**
* IP to long
* @param type $ip
* @return int
*/
function ipToLong($ip = false) {
if (!$ip) {
$ip = getClientIPAddress();
}