Upload Multiple Files and Store 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 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]

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).

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.

Embed HTML Inside PHP If Statement

AmitDiwan
Updated on 06-Apr-2020 07:27:27

6K+ Views

Yes, HTML can be embedded inside an ‘if’ statement with the help of PHP. Below are a few methods.Using the if condition −    it is displayed iff $condition is met Using the if and else if conditions −     it is displayed iff $condition is met    HTML TAG HERE    HTML TAG HERE Embedding HTML inside PHP − HTML TAG HERE

Detect Search Engine Bots with PHP

AmitDiwan
Updated on 06-Apr-2020 07:21:13

1K+ Views

A search engine directory of the spider names can be used as a reference. Next, $_SERVER['HTTP_USER_AGENT']; can be used to check if the agent is a spider (bot).Below is an example demonstrating the same −if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "some_bot_name")) {    //other steps that need to be used }Code explanation − The agent, along with the user agent is passed to the strtolower function, whose output in turn is passed to the strstr function. Both the user agent and the bot are compared to see if the spider is a bot or not.Another option is shown below −function _bot_detected() {    return (   ... Read More

Instantiate Object and Call Method in PHP on Same Line

AmitDiwan
Updated on 06-Apr-2020 07:06:10

910 Views

Yes, an object can be instantiated and a method can be called on a single line using PHP. This feature has come into effect beginning from PHP version 5.4.An object can be instantiated by accessing the class member of the class. This can be seen in the below snippet −(new my_var)-> my_instance()Code explanation − Here, my_instance is the method and my_var is the object that needs to be instantiated.Example Live Democlass Test_class {    public function __construct($param) {       $this->_var = $param;    }    public function my_method() {       return $this->_var * 2;    }   ... Read More

Print Newline in PHP Using Single Quotes

AmitDiwan
Updated on 06-Apr-2020 07:03:11

637 Views

Since can’t be used with single quotes, we need to resort to other options.When using command line interface, the constant PHP_EOL can be used.When using with browsers, the ‘’ can be used.Both the options have been demonstrated below.Suppose our option was not cli, the ‘else’ part will be executed and a newline will be printed −Example Live DemoOutputThis will produce the following output −hi hello hihello

Break an Outer Loop in PHP

AmitDiwan
Updated on 06-Apr-2020 06:59:59

235 Views

If there are two nested loops, the break statement can be used −break 2;Below is a demonstration with the foreach loop −foreach(...) {    foreach(...) {       if (my_var_1.name == my_var_2)       break 2; //it breaks out of the outermost foreach loop    } }For PHP version>=5.3, the below lines of code can be used −foreach (...) {    foreach (...) {       if (my_var_1.name == my_var_2)       goto top;    } } top:

Print Previously Typed Snippets in JShell in Java 9

raja
Updated on 03-Apr-2020 17:07:48

83 Views

JShell is an official Read-Evaluate-Print-Loop (REPL) introduced in Java 9. It provides an interactive shell for quickly prototyping, debugging, and learning Java and Java API without the need for a main() method.The "/list" command in JShell prints out all of the previously typed snippets of that particular session with a unique identifier called the snippet ID. By default, the output doesn't contain any snippet with only valid statements or expressions that can be shown. We need to see all previously typed code include errors, then pass the -all argument to the /list command.In the below code snippet, we have created ... Read More

Advertisements