1

I m trying to run php script using cmd

CMD Code

"C:\wamp\bin\php\php5.4.16\php.exe" -f "C:\wamp\www\file\test.php"

PHP Code

$fileLocation = getenv("DOCUMENT_ROOT") . "/file/myfile.txt";//OR $fileLocation = "myfile.txt"
$file = fopen($fileLocation,"w");
$content = "Your text here";
fwrite($file,$content);
fclose($file);

Here problem is file is not created. If I run this file in browser then file is generate. user is administrator .

1
  • I don't think getenv("DOCUMENT_ROOT") renders anything useful... Echo that value and check if the result is what you were hoping it to be. Commented Dec 8, 2014 at 7:54

1 Answer 1

1

To run php code try this command:

php -f yourfilename.php

if you are in different location than specify absolute path. This will only work when you set your environment variable properly.

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

3 Comments

replace '/file/myfile.txt' to '\file\myfile.txt' than it will work. actually it can not able to find the path of file.
set environment variable and your cmd type php -q pathtophpfile/yourfile.php
I run this on my localhost and its working properly. Code was like $file = fopen('D:\xampp\htdocs\file\hello.txt','w'); i faced problem when i assign path to a php variable.

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.