简体   繁体   English

使用php oncli的几个问题〜使用XAMPP

[英]Using php oncli few questions ~ using XAMPP

Attempting to expand my knowledge by using PHP on the Command Line. 尝试通过在命令行上使用PHP扩展我的知识。

Currently I have a default installation of XAMPP, and have set up my Environment Variable. 当前,我具有XAMPP的默认安装,并设置了我的环境变量。

I've been able to execute simple scripts like: 我已经能够执行简单的脚本,例如:

<?php echo 'hello world!'; ?>

Questions ~ 问题〜

Where do I store the scripts I am using? 我要在哪里存储所使用的脚本? Currently I am doing: 目前我正在做:

C:\\Users\\Ross>php c:\\helloworld.php C:\\ Users \\ Ross> php c:\\ helloworld.php

it works. 有用。 Does this mean I need to specify a path every time? 这是否意味着我每次都需要指定路径? Or should I store php files inside my c:>xampp\\php directory? 还是应该将php文件存储在c:> xampp \\ php目录中? I tried this and it doesn't appear to work. 我试过了,它似乎没有用。

What would be the accepted "best practice". 什么是公认的“最佳实践”。

2nd question Could someone explain why this doesn't work: 第二个问题可以有人解释为什么这不起作用:

    <?php  
    fwrite(STDOUT, "Enter file name:\n"); 

    $file=fgets(STDIN);
    print 'you entered...' . $file;

    $fp=fopen($file,'r');

        if(!$fp)
        { 
            print 'File could not be opened..'; 
        } 
        else 
        {
            /* show file pointer */
            print($fp);
        }
    ?>

and then I do: 然后我做:

C:\Users\Ross>php c:\file.php
Enter file name:
c:\foo.txt
you entered...c:\foo.txt

Warning: fopen(c:\foo.txt): failed to open stream: Invalid argument in C:\file.php on line 6
File could not be opened..

"foo.txt" is in the same directory and does exist. “ foo.txt”在同一目录中并且确实存在。

thanks for any clarification. 感谢您的澄清。

As far as were to store the files is concerned: I normally add the directory where php.exe is to my PATH environment variable, that way I can just call php in whatever directory contains the script I need to run. 就存储文件而言:我通常将php.exe所在的目录添加到PATH环境变量中,这样我就可以在包含需要运行的脚本的任何目录中调用php。 If you don't add the directory to PATH, then you would need to either run php from its directory and specify the full path to the PHP script, or run it from the directory where the PHP script is and specify the full path to the PHP executable. 如果不将目录添加到PATH,则需要从其目录运行php并指定PHP脚本的完整路径,或者从PHP脚本所在的目录中运行它并指定该脚本的完整路径。 PHP可执行文件。

Regarding opening the file: the reason this is occurring is because fgets is returning the newline from you pressing enter, too (it would seem). 关于打开文件:发生这种情况的原因是fgets也从您按Enter的位置返回换行符(看来)。 So in reality, it's trying to open a file whose name actually ends with a new line character. 因此,实际上,它试图打开一个文件名实际上以换行符结尾的文件。

Change the line: $file=fgets(STDIN); 更改行:$ file = fgets(STDIN);

to: $file=trim(fgets(STDIN)); 到:$ file = trim(fgets(STDIN));

and you should be fine. 你应该没事的

question #1: all your php files should be inside the www folder of xampp (c:\\xampp\\www) 问题#1:您所有的php文件都应位于xampp的www文件夹内(c:\\ xampp \\ www)

question #2: probably because you are not working in the correct folder. 问题2:可能是因为您使用的文件夹不正确。

xammp is good but I recommend you to use wamp, it's much easier to understand and use. xammp很好,但是我建议您使用wamp,它更易于理解和使用。 Just google for it. 只是谷歌它。 xampp is more for those who are more techically skilled. xampp更适合那些技术娴熟的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM