Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
PHP Articles
Page 65 of 81
Make PHP pathinfo() return the correct filename if the filename is UTF-8
Most of the core PHP functions don’t deal with character sets apart from Latin-1. But before the ‘pathinfo’, placing the ‘setlocale’ can be used to return the correct filename even if it is UTF-8 encoded.By default, it runs with ‘C’ locale, and CLI scripts run with a default utf-8 locale. The locale on the server should be changed from ‘C’ to ‘C.UTF-8’ or to ‘en_US.UTF-8’ before calling the other functions.setlocale(LC_ALL,'en_US.UTF-8'); pathinfo($OriginalName, PATHINFO_FILENAME); pathinfo($OriginalName, PATHINFO_BASENAME);
Read MoreHow do I iterate through DOM elements in PHP?
Following is the XML data (input) − Iterating through the elements in the DOM object.Example$elements = $dom->getElementsByTagName('foo'); $data = array(); foreach($elements as $node){ foreach($node->childNodes as $child) { $data[] = array($child->nodeName => $child->nodeValue); } }OutputThis will produce the following output −Every ‘foo’ tag will be iterated over and specific ‘bar’ and ‘pub’ value will be obtained, i.e specific child nodes can be accessed by their names itself.The elements in the XML file are obtained by running a foreach loop over ...
Read MoreWhat does the '@' prefix do in PHP?
The ‘@’ symbol suppresses errors from getting displayed on the screen.PHP supports an error control operator, i.e the sign (@). When it is prepended to an expression in PHP, error messages that could get generated when it uses that expression will be ignored.If the track_errors attribute is enabled, the error message generated by the expression will be saved in the variable named $php_errormsg. This variable will be overwritten each time on every error.It is always suggested to write code that works relevantly with error states/conditions.
Read MoreHow to open an Excel file with PHPExcel for both reading and writing?
There is no concept of opening a file for read and write in PHPExcel since it is not aware of the source of the PHPExcel object. Irrespective of the source from where the file was loaded or the type of file, the file can be read based on its named and saved with the same name. This way, the file will be overwritten, and new changes will be reflected in the file. Example error_reporting(E_ALL); set_time_limit(0); date_default_timezone_set('Europe/London'); set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/'); include 'PHPExcel/IOFactory.php'; $fileType = 'Excel5'; $fileName = 'name_of_file.xls'; // Read the file $objReader = PHPExcel_IOFactory::createReader($fileType); $objPHPExcel = ...
Read MoreWill enabling XDebug on a production server make PHP slower?
Yes, debuggers like XDebug reduce the performance of the PHP server. This is the reason why debuggers are not placed in server environment. They are deployed in a different environment to avoid unnecessary overheads.Debug messages can’t be displayed in an application that is already in the production phase.When debugging behavior is added to the server, the debug engine gets attached to the PHP process. It starts to receive messages to stop at breakpoint, but this is not the required behavior, since it introduces a high-performance blow to other processes thereby stopping the PHP parser.On the other hand, when a debugger ...
Read MoreHow can I extract or uncompress gzip file using php?
The zipped files can be unzipped or decompressed using PHP’s gzread function. Below is the code example for the same − Example $file_name = 'name_of/.dump.gz'; $buffer_size = 4096; // The number of bytes that needs to be read at a specific time, 4KB here $out_file_name = str_replace('.gz', '', $file_name); $file = gzopen($file_name, 'rb'); //Opening the file in binary mode $out_file = fopen($out_file_name, 'wb'); // Keep repeating until the end of the input file while (!gzeof($file)) { fwrite($out_file, gzread($file, $buffer_size)); //Read buffer-size bytes. } fclose($out_file); //Close the files once they are done with gzclose($file); Output This will ...
Read MoreWhy is PHP apt for high-traffic websites?
The main reason behind websites being slow is overloading of hosts.The benefit of using PHP over other compiled languages is the ease of maintenance. PHP has been designed ground up to efficiently handle HTTP traffic, there is less to build in comparison to building using other compiled languages. In addition to this, merging changes is an easier task since the server doesn’t need to be recompiled and restarted (which needs to be done in case of using a compiled binary- FastCGI).PHP, when properly written, can be scaled to a great extent. Other limiting factors include the database engine that is ...
Read MoreHow to redirect domain according to country IP address in PHP?
The GeoIP extension can be used to find the exact location of an IP address. Apart from this, the geoPlugin class can be downloaded from −https://www.geoplugin.com/_media/webservices/geoplugin.class.phpsThe country code list can be found in the below link −https://www.geoplugin.com/iso3166An index.php file can be placed inside the root folder and the below lines of code can be put inside this index file −Once the geoplugin class has been downloaded, a new instance is created and given the name ‘geoplugin’. The locate function is called on this instance of the geoplugin class. The same class object’s countryCode is assigned to a variable named ‘var_country_code’. ...
Read MorePHP: How do I display the contents of a textfile on my page?
The file_get_contents function takes the name of the php file and reads the contents of the text file and displays it on the console. get the contents, and echo it out.The contents of filename.php would be the output.In the above code, the function ‘file_get_contents’ is called by passing the php file name. The output will be the contents present in the php file.
Read MoreRead last line from file in PHP
To read last line from file in PHP, the code is as follows −$line = ''; $f = fopen('data.txt', 'r'); $cursor = -1; fseek($f, $cursor, SEEK_END); $char = fgetc($f); //Trim trailing newline characters in the file while ($char === "" || $char === "\r") { fseek($f, $cursor--, SEEK_END); $char = fgetc($f); } //Read until the next line of the file begins or the first newline char while ($char !== false && $char !== "" && $char !== "\r") { //Prepend the new character $line = $char . $line; fseek($f, $cursor--, SEEK_END); $char = fgetc($f); ...
Read More