Found 1060 Articles for PHP

Run a Python program from PHP

SaiKrishna Tavva
Updated on 10-Sep-2024 16:38:20

7K+ Views

In PHP, the 'exec()' or 'shell_exec’ function can be used. It can be executed via the shell and the result can be returned as a string. It returns an error if NULL is passed from the command line or returns no output at all. 'exec()' Function The exec function is used for executing commands in the shell and command line of an external program and optinally capture the last line of the output. Syntax exec(string $command, array &$output = null, int &$return_var = null); 'shell_exec()' Function The shell_exec is similiar to exec but it captures and return the ... Read More

How to effectively hide Href From a Link in PHP?

AmitDiwan
Updated on 06-Apr-2020 08:20:48

778 Views

This is not possible. A href can’t be hidden from a link. But the files can be rewritten and the request URL can be changed to look like this − name.php/5001Other than this, a post request can be used in the below way − Go This will expose a single button in the browser.

PHP script to get all keys from an array that starts with a certain string

AmitDiwan
Updated on 06-Apr-2020 08:18:02

1K+ Views

Method 1$arr_main_array = array('test_val' => 123, 'other-value' => 456, 'test_result' => 789); foreach($arr_main_array as $key => $value){    $exp_key = explode('-', $key);    if($exp_key[0] == 'test'){       $arr_result[] = $value;    } } if(isset($arr_result)){    print_r($arr_result); }Method 2A functional approach An array_filter_key type of function is taken, and applied to the array elements $array = array_filter_key($array, function($key) {    return strpos($key, 'foo-') === 0; });Method 3A procedural approach −$val_1 = array(); foreach ($array as $key => $value) {    if (strpos($key, 'foo-') === 0) {       $val_1[$key] = $value;    } }Method 4A procedural approach using ... Read More

How to resize image in PHP?

AmitDiwan
Updated on 06-Apr-2020 07:55:41

5K+ Views

Images can be resized using ImageMagick or GD functions. If GD’s functions are used, the size of the image file is also reduced when raw digital camera images are sampled. We will see how GD can be used to resize an image in the below code.function image_resize($file_name, $width, $height, $crop=FALSE) {    list($wid, $ht) = getimagesize($file_name);    $r = $wid / $ht;    if ($crop) {       if ($wid > $ht) {          $wid = ceil($wid-($width*abs($r-$width/$height)));       } else {          $ht = ceil($ht-($ht*abs($r-$w/$h)));       }     ... Read More

Sort multidimensional array by multiple keys in PHP

AmitDiwan
Updated on 06-Apr-2020 07:53:22

3K+ Views

The array_multisort function can be used to sort a multidimensional array based on multiple keys −Example$my_list = array(    array('ID' => 1, 'title' => 'data one', 'event_type' => 'one'),    array('ID' => 2, 'title' => 'data two', 'event_type' => 'zero'),    array('ID' => 3, 'title' => 'data three', 'event_type' => 'one'),    array('ID' => 4, 'title' => 'data four', 'event_type' => 'zero') ); # The list of sorted columns and their data can be obtained. This will be passed to the array_multisort function. $sort = array(); foreach($my_list as $k=>$v) {    $sort['title'][$k] = $v['title'];    $sort['event_type'][$k] = $v['event_type']; } # ... Read More

Convert Dashes to CamelCase in PHP

AmitDiwan
Updated on 06-Apr-2020 07:38:16

1K+ Views

Following is the code to convert dashes to CamelCase in PHP −Sample input − this-is-a-test-stringSample output − thisIsATestStringNote − There is no need to use regex or callbacks. It can be achieved using ucwords.function dashToCamelCase($string, $capitalizeFirstCharacter = false) {    $str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));    if (!$capitalizeFirstCharacter) {       $str[0] = strtolower($str[0]);    }    return $str; } echo dashToCamelCase('this-is-a-string');For PHP version>=5.3, the below code can be used −function dashToCamelCase($string, $capitalizeFirstCharacter = false) {    $str = str_replace('-', '', ucwords($string, '-'));    if (!$capitalizeFirstCharacter) {       $str = lcfirst($str);    } ... Read More

How to upload multiple files and store them in a folder with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:36:51

17K+ Views

Below are the steps to upload multiple files and store them in a folder −Input name must be defined as an array i.e. name="inputName[]"Input element should have multiple="multiple" or just multipleIn the PHP file, use the syntax "$_FILES['inputName']['param'][index]"Empty file names and paths have to be checked for since the array might contain empty strings. To resolve this, use array_filter() before count.Below is a demonstration of the code −HTMLPHP$files = array_filter($_FILES['upload']['name']); //Use something similar before processing files. // Count the number of uploaded files in array $total_count = count($_FILES['upload']['name']); // Loop through every file for( $i=0 ; $i < $total_count ; ... Read More

Extract a property from an array of objects in PHP

AmitDiwan
Updated on 06-Apr-2020 07:34:36

4K+ Views

Given the below code, the task is to extract the ID of the my_object variable −Example$my_object = Array ( [0] => stdClass Object    (       [id] => 12    ),    [1] => stdClass Object    (       [id] => 33    ),    [2] => stdClass Object    (       [id] => 59    ) )The array_map function can be used for older versions of PHP. Below is a demonstration of the same.$object_id = array_map(create_function('$o', 'return $o->id;'), $objects);For PHP version 5.5 or higher, the array_column function could be used. Below is a demonstration of the same −$object_id = array_column($my_object, 'id');OutputThis will produce the following output −[12, 33, 59]

How to identify server IP address in PHP?

AmitDiwan
Updated on 06-Apr-2020 07:31:25

4K+ Views

The server IP can be identified with the below line of code −$_SERVER['SERVER_ADDR'];The port can be identified using the below line of code −$_SERVER['SERVER_PORT'];For PHP version 5.3 and higher, the following lines of code can be used −$host_addr= gethostname(); $ip_addr = gethostbyname($host_addr);This can be used when a stand-alone script is being run (which is not running via the web server).

How to force file download with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:30:32

2K+ Views

The below code can be used to force a file to get downloaded in PHP.

Advertisements