Current path in PHP on Windows (standalone CLI) - windows

I was trying to get path current path in PHP. I tried looking though phpinfo();, but I haven't found any interesting values which could be used to get path to my script. There is no nice values which I used on Linux, like $_SERVER["PWD"].
Now I'm wondering how I'm supposed to find current path. Maybe some function will work... I really have no idea. Because I don't want to hardcode path to script.

getcwd() is what you are looking for.

It's not entirely clear whether you mean the current working directory, or the path to the current script. For the working directory, see #Taze's answer.
For the current script, the __FILE__ magic constant will give you the full filesystem path to the current file.
Note that these constants take "current file" literally: If you include a file and call __FILE__ there, it will show the included file's path.

The getcwd() method will return the current working directory on success.
<?php
echo getcwd() . "\n";
?>

Related

Get current path with äöüè in name (__FILE__)

Using Windows, I've experienced a slight annoyance when using __FILE__ to get the current location of a file or the absolute path of another file with
File.expand_path("lib/other", File.dirname(__FILE__))
This doesn't work though if the folder has characters like äöüè and similar. This get's especially annoying if the windows username of a client contains such a character and my script necessarily lives inside the %appdata% folder.
To demonstrate my problem, C:\äüé\test.rb contains only
puts __FILE__
Running it:
> ruby C:\äüé\test.rb
C:/"?'/test.rb
Is there a reliable way to get the current file path?

What is the use of expand_path

I came across the following line of code
#base_dir = File.expand_path(".")
Can anyone tell what the use is of expand_path and the "." as a parameter?
File.expand_path() receives a directory (like "." that denotes the current directory) and returns the absolute path of that same directory.
So in this case it will return the absolute path of the current working directory.
Look here the documentation of this method:
http://ruby-doc.org/core-1.9.3/File.html

Why load "file.rb" works even though "." is not in the load path?

I have created a project in /Projects/test that have the following files:
/Projects/test/first.rb
/Projects/test/second.rb
In first.rb, I do:
load 'second.rb'
And it gets loaded correctly. However, if I open the console and I type $:, I don't see the current directory "." in the load path. How does Ruby know where to load that 'second.rb' from?
See the documentation of Kernel#load clearly :
Loads and executes the Ruby program in the file filename. If the filename does not resolve to an absolute path, the file is searched for in the library directories listed in $:. If the optional wrap parameter is true, the loaded script will be executed under an anonymous module, protecting the calling program’s global namespace. In no circumstance will any local variables in the loaded file be propagated to the loading environment.
In case load 'second.rb' - second.rb has been internally resolved to the absolute path /Projects/test/second.rb,as your requiring file in the directory is same as required file directory. Nothing has been searched to the directories listed in$: for your case.
Just remember another way always
- The load method looks first in the current directory for files
Contrary to the currently accepted answer, the argument 'second.rb' does not resolve to an absolute path. If that were what was meant, you would also be able to require 'second.rb', since require has exactly the same wording about absolute paths.
I think what's happening here is just that the phrasing in the documentation for load is not clear at all about what the actual steps are. When it says "Loads and executes the Ruby program in the file filename," it means that literally — it treats the argument as a file name and attempts to load it as a Ruby program. If isn't an absolute path†, then Ruby goes through $LOAD_PATH and looks for it in those places. If that doesn't turn anything up, then it just goes ahead and tries to open it just as you passed it in. That's the logic that MRI actually follows.
† The actual check that Ruby does is essentially "Does the path start with '/', '~' or './'?".

Problems with Ruby/Gosu relative file referencing

So I'm making a game with Ruby/Gosu and the lines to load all the images look like this:
#image_name = Gosu::Image.new(self, 'C:\Users\Carlos\Desktop\gamefolder\assets\bg.jpg', false)
I want to refer to them based on their location relative to the referring file. The file which includes the above line is in C:\Users\Carlos\Desktop\gamefolder\, so I would think I could just change the above to '\assets\bg.jpg' or 'assets\bg.jpg', but this doesn't work.
The specific error is "Could not load image assets/bg.jpg using either GDI+ or FreeImage: Unknown Error (Runtime Error)."
If you want to get the current directory (of your execution context, not necessarily the file you're 'in'), just use Dir.pwd. Output this to console to check that your current directory is actually gamefolder.
To get the current directory of your actual ruby file (relative to Dir.pwd), use __FILE__, e.g.
File.dirname(__FILE__)
Pass that to File.expand_path to get a fully-qualified path. You can do a little sanity check by making sure File.exists?("#{File.expand_path File.dirname __FILE__}/assets/bg.jpg") returns true.
(Try File.expand_path('assets/bg.jpg')...that might be all you need here.)

Checking if a file exists in the user's home directory

How would I, say, determine if the file ~/.my_proj_config exists on any OS in Ruby?
A call to Dir.home is a OS independent way to get to the home directory for the user. You can then use it like
File.exists?(File.join(Dir.home, ".my_proj_config"))
This works in Ruby 1.9, but note that the call to expand_path is required on some systems (e.g. Windows):
File.exists?( File.expand_path "~/.my_proj_config" )
Use the class File and its method exist?.
Take a look at the Pathname class, specifically the realpath function - This will get you the full (expanded) path to your file.
http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/classes/Pathname.html#M001991
You then use the File class along with exists? method to find out if that exists. You shouldn't need to use realpath if you use this method, however.
http://www.ruby-doc.org/core/classes/File.html#M000045

Resources