I cant seem to get the realpath function to work with variables is there a way I can fix this? When I use variables in the function it returns nothing I know the pathname works
Well it isn't finished yet, but it fits perfectly...
Look. Your site is living in two realms at a time: A real one and a virtual one.
For site users it's a virtual web-server, and you must understand a very important thing about it: There are no files on a web-server. Imagine you type http://example.com/index.html
in the address bar. It's not a file nor path, but a URL, an HTTP resource. A virtual address, completely different from a regular file.
While for you as a site developer, a web-server is a program running on the computer. This computer most likely has a hard disk and a filesystem. Your PHP script, while dealing with data or including other scripts, manipulates files on a physical disk.
Such a difference is a very common pitfall for beginners.
Many of them confuse a file with a hyperlink, trying to call a local file using HTTP protocol or include a file using virtual path from the web-server root.
Moreover, there are two things that hinders proper understanding:
Be cautious and do not let them fool you.
Well, after all it's not rocket science. One just has to understand these 3 matters:
root
thing and what's the difference between filesystem root and web-server rootThe first one can be easily distinguished:
A file belongs to filesystem. It's a piece of data stored on hard disk. It has several attributes, such as size, modification time, owner, permissions and so on. It's explicitly filesystem attributes, no HTTP resource has it.
Both data files and your scripts are files, and should be treated as such. If you do something like include 'http://yoursite.com/file.php';
, you're not including PHP code as expected, but just plain HTML - the result of file.php being executed!
If you want to read image names from directory, do not read it from http://yoursite.com/image/
. It's not a directory.
On the other hand, HTTP resource is HTTP protocol thing. Your web-server is a daemon that listens for requests. Upon receiving a request for a particular resource identified by URI, it looks for this resource, and (if found) returns its contents accompanied by several HTTP headers.
A web-server has its own directory structure but it is very important to understand that this directory structure is virtual. It just resembles a real filesystem but it is not a filesystem at all.
/dir/
, /dir/file.ext
,/dir/whatever
- while all these paths looks different, they are all the same.
It is tricky subject but very important to understand.
There are no files or directories in the HTTP. But just resources, which consists of headers and body.
If you request /dir/
URI from the server, you get headers and a body.
If you request /dir/file.ext
URI from the server, you get a headers and a body.
Every time you request something from the server, you get a headers and a body.
The only exception possible is a response that includes headers but no body.
While some resources may resemble files, they are not files. They have no attributes, but headers. It's really different realm.
Of course, most of time web-servers just find a physical file corresponding to requested URI and output it. But it's not a file anymore but only the file's contents, preceeded by some headers.
Yes - that's the difference: It's most important to distinguish a file as a filesystem subject and it's contents.
If you request something that looks like as a directory, you get in reality... an HTML code! It is very important to distinguish a directory in terms of filesystem form HTML code preceded by some headers. (Of cource you can get not HTML but some binary file contents - it's just a matter of Content-type header. Another reason not to confuse real world with virtual one)
Conclusion:
If you want your script to access a file directly - address it as a file. Do not call it via HTTP! (Note that your php scripts are files ;-)
If you want the same file to be accessed by a client using HTTP protocol - address it as a virtual resource.
Well, root is a root. A starting point. A place where all begins.
Both a filesystem and a web-server have their own roots.
On Unix systems a filesystem root is /
. Note that it is not just a directory separator! If a path begins from that symbol, it's a name of particular directory - a root one. Every other file and directory belongs to root.
On windows system it's quite messy (as usual). There is no common filesystem root, but separate disks with it's own roots. It starts from the disk drive letter and a backslash: C:\
is a root of drive C.
A web-server has it's own root. A place where virtual world meets real one. From the server's point of view, web-server root (often called DOCUMENT_ROOT) is a filesystem directory, where the web-server looks for the files representing HTTP resources. For the client it's full qualified root - a place where the web-site begins. It looks like Unix-system root - /
. Client knows nothing of server's filesystem. For the client this /
is the only root.
Conclusion:
Always know where your root is.
For the web-server it's simple: It's always /
.
For the filesystem know the point where virtual root meets real world - a DOCUMENT_ROOT. It will help you to find your own files.
That's simple.
Actually, only absolute paths are real. While relative ones are imaginable and every relative path always translates into abaolute one before use.
An absolute path is inseparably linked with root
. As a matter of fact, an absolute path is a path that built from the root:
As you can see, the first two are filesystem paths, from unix and windows respectively, while the third one can't be clearly defined, but is most likely a virtual HTTP resource.
Every path which includes no root directoctory is a relative one. It builds up from the current location:
Conclusion:
Always use absolute paths when possible. It's unambiguous and works everywhere, regardless of current directory. It may contain variable part, to let it work in different environments, but at the end it should be always absolute.
Does the path really exist? As for the realpath function to work, the path has to be a valid and existing location.
So if you are using .htaccess to create "pseudo" paths, the realpath function will not work on them. The use of variables inside should not matter (given that they are populating fine and are correct), as all it really requires is a valid string.
realpath() returns FALSE on failure, e.g. if the file does not exist.
http://php.net/realpath
Check your variables, dude