Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Capital Tutorials Photoshop Star Adobe Tutorials N1 Studios 

Change Language

Who's online

There are currently 0 users and 5 guests online.

You can open a file from a path

It can be a path like "html/website/index.html" for scripts in directories below the curent php script running this code.

<?php
// Store the file name in a string.
$filename "dir/www/file.txt";

// Read file into array
$contents file($filename);
?>

Now if you need to get the contents of a file that is in a different directory on the same level (or higher) than the directory with the php script you would use the Unix statement "../" before your file path. Let me demonstrate. lets say the php file script is in "dir1" but we want to read a text file in "Subdirofdir2":

>website

>>dir1 <-PHP Script
>>>Subdirofdir1

>>dir2
>>>Subdirofdir2 <-Text File

We can't use the path "dir2/subdirofdir2/text.txt" because there is no path like that in the current php script's directory. (the php script is in "dir1" remember). However if we could get to the "website" directory (which is the root) we could then use the "dir2/subdirofdir2/text.txt" path and it would work. So if we add the unix "../" before the path php will look for the path in the directory 1 above the current directory path (which is "website").

So our path from the php script to the text file should be:

<?php
// Go up one level - then look for the "dir2" directory.
$filename "../dir2/subdirofdir2/text.txt";

// Read file into array
$contents file($filename);
?>


Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati