Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Query regarding / and \ (http://www.programmingforums.org/showthread.php?t=15824)

grimpirate May 13th, 2008 7:40 PM

Query regarding / and \
 
Dependent on what OS and server PHP is running under there may be a difference between URIs to locate files. This comes from using the backward slash \ (as in Windows) and a forward slash / (as in Unix). My question is how could I determine which slash would be proper to use without knowledge of where the script is going to be executed? I imagine that I could probably execute a character count on the __FILE__ magic constant. That just seems like I'm looking for trouble though. Any other suggestions?

Sane May 13th, 2008 7:46 PM

Re: Query regarding / and \
 
It sounds like you're trying to directly approach a piece of some problem instead of looking for the solution to the whole. What is it you need to accomplish? If you're trying to cut the filename from the URI, there are functions in PHP to do just that (look at basename and related functions).

grimpirate May 13th, 2008 7:57 PM

Re: Query regarding / and \
 
Not at all, the issues is that I need to create a filename for a file that doesn't yet exist. So let's say something like I used the mkdir() command to create 'somedir'. That's simple enough as it will create 'somedir' on whatever directory the script is currently executing. However, if I want to create a file within 'somedir' how would I know which of the two slash types to use? i.e.

somedir/somefile.txt
somedir\somefile.txt

Sane May 13th, 2008 8:11 PM

Re: Query regarding / and \
 
Hmm, are you positive? I would expect PHP to work with "somedir/somefile.txt" no matter the OS. Have you tested it to be sure? I've opened up some installation scripts from some open-source PHP software I have, and they all use '/' when writing to files in higher subdirectories.

grimpirate May 13th, 2008 11:29 PM

Re: Query regarding / and \
 
Yeah forward slashes work everywhere. Apparently only windows uses the backslash, but I guess the PHP designers made it to work with forward slashes as well. it would appear. So I guess I'll just have to use an str_replace() if I happen to encounter a filename with backslashes. I just wanted to stick with what the OS would natively support.

titaniumdecoy May 13th, 2008 11:59 PM

Re: Query regarding / and \
 
I believe Windows "natively supports" forward slashes, since typing a path with forward slashes into Windows Explorer works just fine.

MiKuS May 14th, 2008 2:48 AM

Re: Query regarding / and \
 
using / in the command prompt does not work for me, is php going to use explorer.exe to do it's file operations?

do not use $_SERVER['OS'] or $_ENV['OS'] you're better off using the PHP_OS constant.
try this:

on my computer it says "Dar" for Darwin

:

<?php
if (substr(PHP_OS,0,3)=='WIN') {
  // code for windows
}
?>


Sane May 14th, 2008 7:02 AM

Re: Query regarding / and \
 
Quote:

Originally Posted by MiKuS (Post 145170)
is php going to use explorer.exe to do it's file operations?

I was guessing that PHP internally handles it based on the OS, the same way many modern programming languages will often have functions that automatically handle the cross-platform conversion (IE, Sockets are no different in Windows or UNIX when working with Python). Just my guess though, since PHP seems to be a pretty friendly language.

MiKuS May 14th, 2008 7:26 AM

Re: Query regarding / and \
 
this is true, php handles it's own file operations. I was trying to point out that because explorer.exe can handle / in the address doesn't necessarily mean PHP can, i used the console to show that / is definitely not native to windows (although in most cases can deal with / )

kruptof May 14th, 2008 8:31 AM

Re: Query regarding / and \
 
why not just test which slash the path contains?


All times are GMT -5. The time now is 4:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC