PHP file exists - simple check
PHP file exist : PHP file exist is easily way to check if file exist on server or not. if you are running on website , you need user root directory for proper execution ..
Use below code :
Use below code :
<?php
$file=$_SERVER['DOCUMENT_ROOT']."/images/340x150/audi.jpg";
echo file_exists($file) ;
?>
If file exists on server , it will show you output - 1
if file doesn't exists on server, result will null
Please note below script may not work- avoid to use direct URL. Use only root directory.
echo file_exists("http://autoindianews.com/images/340x150/audi.jpg") ;
PHP str_replace() function - replace part of string from existing string
Syntax: str_replace("<replace from string>","<replace to string>","<source string>");
Replace
the characters "I am" with "You are" from the sentence "I am beautiful."
<?php
echo str_replace("I","You","I am beautiful.");
?>
echo str_replace("I","You","I am beautiful.");
?>
Output: You are
beautiful.
Error Handling in PHP Simple Trick
Set error handler at beginning, write error handler function to track error . you can write one more function to save error in error log file.
//set error handler at beginning
set_error_handler("errorHandler ");
<?php
//error handler function
function errorHandler($errno, $errstr)
{
echo "<b>Error Number :</b> $errno";
echo "<b>Error Number :</b> $errno";
echo "<b>Error Message:</b> $errstr";
}
<Your error code here -start >
}
<Your error code here -start >
------
<Your
error code here -End >
?>
Read Current Directory Structure in PHP
Syntax: Read folder structure from root directory : getcwd();
Output - C:\xampp\htdocs\<YOUR
FOLDER DIRECTORY>
Description: <Your
Folder Directory with root>
Syntax: Read folder structure from root
directory : dirname(__FILE__);
Output - C:\xampp\htdocs\<YOUR
FOLDER DIRECTORY>
Description: <Your
Folder Directory with root>
Syntax: Red only current folder name : basename(__DIR__)
For Example – http://autoindianews.com/car-news/
& code is in “car-news” directory it will return only car-news
Output: car-news <You’re
Folder Directory without root>
Read Full URL in PHP
Here is the syntax, the Double quote is important below, this will show you your current URL
echo $FULL_URL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
To get HTTPS Version, change code with below syntax
$FULL_URL= (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Subscribe to:
Posts
(
Atom
)
No comments :
Post a comment