WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Returns the absolute path of files or folders, works in Linux/Windows/Mac.
Categories : PHP, Filesystem, Beginner Guides Click here to Update Your Picture
Alix Axel
Date : Jun 19th 2008
Grade : 3 of 5 (graded 2 times)
Viewed : 5227
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Alix Axel
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
Like this code?
Show the author your appreciation.
 

<?php

function Path($path)
{
    if (
file_exists($path))
    {
       
$path = str_replace('\\', '/', realpath($path));

        if (
is_dir($path))
        {
            if (
substr($path, -1) != '/')
            {
               
$path .= '/';
            }
        }

        return
$path;
    }

    return
false;
}

// Examples:

Path('P:\xampp\htdocs'); // P:/xampp/htdocs/
Path('P:\xampp\htdocs\index.php'); // P:/xampp/htdocs/index.php
Path('/home/user123/index.php'); // /home/user123/index.php

?>



PHP based Contact email form with multiple recipients, text file based, supports departments.
Categories : PHP, Email, Beginner Guides, Filesystem
Creating a Language File
Categories : PHP, Beginner Guides, Filesystem
Simple image counter
Categories : PHP, Graphics, Filesystem, Beginner Guides
Link Submition - Allow your visitors to submit links to the site.
Categories : PHP, Arrays, Filesystem, Beginner Guides
Convert a File database into MySQL
Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides
Introduction to Language Files
Categories : PHP, Filesystem, Beginner Guides
Selecting a random line from a text file
Categories : PHP, Filesystem, Arrays, Beginner Guides
A flat file counter
Categories : PHP, Cookies, Filesystem, Beginner Guides
PHP Class to calculate Degrees Minutes Seconds to Decimal Degrees
Categories : PHP, PHP Classes, Geo Related, Beginner Guides
Unix Disk Information with graphs
Categories : PHP, Shell Scripting, Filesystem
A simple configuration file editor to ease you life in setting up php applications. Reads variables from a given file automatically and displays current value. New value will be written to file after submit.
Categories : PHP, Filesystem, Regexps, Java Script
PHP Email image generator - hide your email from bots - using the GD Library
Categories : PHP, Graphics, GD image library, Beginner Guides
Read a file with strings and create a new file with the first half of each string
Categories : PHP, Strings, Filesystem
Newbie Notes #3 - What went wrong? A useful little debugging aid
Categories : PHP, Beginner Guides, Debugging
Search and Replace Text : Searches Files for Specified Text and Replaces It by a Given Text
Categories : PHP, PHP Classes, Search, Filesystem
 Victor Roman wrote : 1780
That function will be written in a more readable and easy way. I assume you're writing a wrapper over realpath and appending the trailing directory separator character (because this function, otherwise, is non-sense).

<?
function Path($path)
{
    
/* Checks if the file exists */
    
if (false == file_exists($path))
            return 
false;

    
/* Now takes the real system file path */
    
$path  realpath($path);

    
/*
     * If the given path is a directory we take care of this and
     * appends the trailing directory separator character
     */
    
if (is_dir($path) && $path[strlen($path)] != DIRECTORY_SEPARATOR)
        
$path .= DIRECTORY_SEPARATOR;

    
/* Returns the translated path */
    
return $path;
}
?>
 
 Alix Axel wrote :1786
The function you have rewritten doesn't seem more readable to me (maybe it's just a matter of taste since you are returning false on the beginning and not at the end, one could argue that testing for more probable conditions at the start is wiser in terms of performance than the inverse).

Anyway your function is in fact useless since you forgot to put (or didn't understand why) str_replace is there - the forwarding slash (/) works on both Windows and *nix environments.

C:\WINDOWS\system32 = C:/WINDOWS/system32