I often find that I have files in my projects that need to be accessed from the file system as well as the users browser. One example is uploading photos. I need access to the files on the file system so that I can use GD to alter the images or move them around. But my users also need to be able to access the files from a URL like example.com/uploads/myphoto.jpg
.
Because the upload path usually corresponds to the URL I made up a function that seems to work most of the time. Take these paths for example:
File System /var/www/example.com/uploads/myphoto.jpg
If I had a variable set to something like /var/www/example.com/
then I could subtract it from the filesystem path and then use it as the URL to the image.
/**
* Remove a given file system path from the file/path string.
* If the file/path does not contain the given path - return FALSE.
* @param string $file
* @param string $path
* @return mixed
*/
function remove_path($file, $path = UPLOAD_PATH) {
if(strpos($file, $path) !== FALSE) {
return substr($file, strlen($path));
}
}
$file = /var/www/example.com/uploads/myphoto.jpg;
print remove_path($file, /var/www/site.com/);
//prints "uploads/myphoto.jpg"
Does anyone know of a better way to handle this?
I always use symlinks in my local development environment and @George's approach fails in this case:
The
DOCUMENT_ROOT
is set to/Library/WebServer/Documents
and there is a symlink/Library/WebServer/Documents/repo1 -> /Users/me/dev/web/repo1
Assume that following codes are in
/Users/me/dev/web/repo1/example.php
while
Thus, replacing
DOCUMENT_ROOT
withHTTP_HOST
doesn't work.I come up with this little trick:
I think it's good practice to fore-prevent the potential bugs even we are not likely to use symlinks in production environment.
All answers here promotes str_replace() which replaces all occurences anywhere in the string, not just in the beginning. preg_replace() will make sure we only do an exact match from the beginning of the string:
Windows can be a problem where directory separators / and \. Make sure you replace the directory separators first:
I would play with something like the following. Make note of realpath() and rtrim().
Make it easy on yourself and just define the correct locations for both the filesystem and web folders and prepend the image filename with them.
Somewhere, you'd declare:
Then you can just swap between paths depending on your need:
For example, i used this one to convert
C:\WAMP\WWW\myfolder\document.txt
tohttp://example.com/myfolder/document.txt
use this one:IMHO such automation is really error prone. You're far better off using some explicit path helpers (eg. one for uploads, one for user pics, etc) or just encapsulate for example an uploaded file with a class.
Assume the directory is
/path/to/root/document_root/user/file
and the address issite.com/user/file
The first function I am showing will get the current file's name relative to the World Wide Web Address.
and would result in:
The second function strips the given path of the document root.
Given I passed in
/path/to/root/document_root/user/file
, I would get