2

I am curious, how can I access a file like this below on my localhost dev server? Is shell even the correct term? Please help

https://github.com/mikesmullin/CSS-Sprite-Generator/blob/master/css-sprite.php

Update:

I am wanting to know how to access PHP files with Cli

3 Answers 3

2

You might want to check out the php.net tutorial, then maybe post specific questions if faced with a particular problem.

Calling a php file is as simple as:

php my_script.php
Sign up to request clarification or add additional context in comments.

1 Comment

Where I am confused is how to get to where I can actually run "php my_script.php" do I need a special program? I tried WinSCP, using the host: localhost but that does not log me on, I tried my windows command prompt as well but that doesn't work...how can I get connected to my localhost server to run such code?
2

If your file is called css-sprite.php, then:

  1. Give this file execution permissions (chmod +x css-sprite.php should do the trick).
  2. Go to the directory of this file and call it directly: ./css-sprite.php.

As you probably see within the example you have provided, the first line contains path to the interpreter - this is similar to the way Python scripts inform about the interpreter that should be used to execute specific file. You should update it to the path that is working for you - just see Example #1 from the documentation:

#!/usr/bin/php
<?php
var_dump($argv);
?>

Does it clarify what you wanted to know?

EDIT:

Of course you can execute your files using command php myscript.php, but the above mentioned solution shows you another way to do that.

Comments

1

Sure you can, and you don't to run a shell process for that. That code is PHP, and if you want to run it from PHP, all you need to do is include it.

Now, the only problem might be how you pass parameters into that script execution. You have 3 (probably more) possibilities:

  1. Modify the script so it doesn't use $_GET for the parameters. I found 11 occurrences of "$_GET" in that file, so this should be easy. You have to check the license, though, if you are allowed to edit the file, and on which conditions.

  2. Set the $_GET variables before including the script. $_GET is a superglobal, so it will be available in the script. But setting $_GET is considered bad practice as you might override some parameters you expect yourself, and in bigger projects this might cause strange results...

  3. Look at the script you linked, look what is being done, and do it yourself. You will save a lot of code (you probably only need 30 of the 300 lines of that file to do what you want) and it will be your own code with which you may do whatever you want. Just don't copy if the license prohibits that. Only crib :P

The latter option is IMHO the best way, because you learn how to do it yourself correctly.*

2 Comments

Thanks for response, however I am trying to learn how to use the CLI part of PHP
You should probably have mentioned that. I don't find it in your original question. Please update it with more information on the context you try to run this script in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.