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 :

<?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") ; 

No comments :

Post a Comment

Leave A Comment...

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.");
?>


Output: You are beautiful.

1 comment :

Post a Comment

Leave A Comment...

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 Message:</b> $errstr";
}



<Your error code here -start >
------
<Your error code here -End >


?>

No comments :

Post a Comment

Leave A Comment...

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__)
& code is in “car-news” directory it will return only car-news
Output: car-news <You’re Folder Directory without root>


No comments :

Post a Comment

Leave A Comment...

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]";


No comments :

Post a Comment

Leave A Comment...