Attempting to expand my knowledge by using PHP on the Command Line.
Currently I have a default installation of XAMPP, and have set up my Environment Variable.
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
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? 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.
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. 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.
Regarding opening the file: the reason this is occurring is because fgets is returning the newline from you pressing enter, too (it would seem). 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);
to:
$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)
question #2: probably because you are not working in the correct folder.
xammp is good but I recommend you to use wamp, it's much easier to understand and use. Just google for it. xampp is more for those who are more techically skilled.
Related
I want to get the contents of my install.sql file that sits one directory up but when I use file_get_contents("../install.sql") it returns false.
How can I fix it?
I have also tried to use fopen and fread.
<?php
// Get the install SQL Files
if (#$install_sql = file_get_contents("../install.sql")){
// Prepare the statement to install
$install_stmt = $conn_test->prepare($install_sql);
// Execute the install
$install_stmt->execute();
} else {
// This is where the code ends up
}
?>
Code shortened
Full code available here
For people suggesting to remove '#' to show errors:
Warning:
file_get_contents(../install.sql): failed to open stream: No such file or directory in
/Users/yigitkerem/Code/SkyfallenSecureForms/Configuration/install.php
on line
55
For me it wasn't making any more sense, that was why I didn't include it, but here we go in case there is something that I don't know.
I have fixed the problem.
I will now briefly describe it here as well.
So this install file was called from another file,
// Unless the installation was complete, the Database Config should exist, if not, run the install.
if((#include_once SSF_ABSPATH . "/Configuration/SecureFormDatabaseConfiguration.php") === false){
// Include the install file
include_once SSF_ABSPATH."/Configuration/install.php";
// Stop further execution
die();
}
I was using the path relative to the install.php file inside the Configuration folder but it turns out the path I used, should have been relative to the file it made the call from.
So to not come across the same problem in the future, I now define a ABSOLUTE PATH from the root file just like WordPress to use as a reference, as expected now I don't need to consider about where the file was called from.
Thanks to #Luuk #tadman for helping me out
I have a script that includes a file and that file has an include in it, like this:
In script:
include('includes/functions/homepage.php);
In homepage.php:
include('includes/functions/parent_functions.php');
I searched here and see it is a very common problem and the solution seems to be to use
include(dirname(__FILE__) . '/includes/functions/homepage.php');
I have the above and still get this error:
Warning: include(includes/functions/parent_functions.php): failed to open stream: No such file or directory
I've tried this on a site running php 5.5 and another using 7.2 - fails the same on both. If I print the path using the following it shows the correct full path.
echo dirname(__FILE__) . '/includes/functions/homepage.php';
As mentioned, this is a common question here but the fix isn't working in my case. Can anyone see why?
Well, you have an issue here. As the parent_functions.php and homepage.php are in the same directory you need not put an extra directory prefix. You can simply use
include('parent_functions.php');
instead of
include('includes/functions/parent_functions.php');
May I ask a dumb question? I have already checked the answers for the same error such as. PHP - Failed to open stream : No such file or directory. and couple of others.
However, this one seems more elmentary problem. I am using XAMPP and I have checked the the property of csv file and it says the location. C:\xampp\htdocs
Then, I checked http://localhost/Untitled-2.php and it reutrns:
Warning: file(Book 1.csv 1): failed to open stream: No such file or directory in C:\xampp\htdocs\Untitled-2.php on line 2
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Untitled-2.php on line 3
Why am I getting these error? My code is the following script:
<?php
$read = file("Book 1.csv 1");
foreach($read as $line){
echo $line .",";
}
?>
So, I think you have a problem with relative and absolute paths.
file("foo.bar") will try to open the file foo.bar relative to your current path. If you ran your php script from the command line, it will try to open the file in the directory you were in when you ran php.
So if you have the following directory structure:
/foo/
/foo/test.php
/foo/bar.csv
/bar.txt
And you are running php from your root directory:
/ $ php /foo/test.php
The working directory of your file will be /, even though the php file itself was in /foo. So if you do
file('bar.csv');
What php will try to open is in fact /bar.csv which doesn't exist.
Please note that what will be the relative working directory of your script can greatly differ in how your script were ran. If it is running from php-fpm mode, it will depend on your config.
To test what your current working directory is, do
echo getcwd();
And then you can list the contents of your working directory with
$files = glob(getcwd() . DIRECTORY_SEPARATOR, GLOB_NOSORT);
var_dump($files);
Or you could do something similar with readdir
All in all, I think you should try to avoid using relative file paths, as it can open up all sorts of problems. If possible, try to use absolute paths instead, like:
file('/var/www/file.csv');
I'm assuming your filename is Book 1.csv because it makes more sense. Your code might have a typo, as the file source is currently Book 1.csv 1 and this could be the cause of your problem.
Try this:
$read = file("Book 1.csv");
I'm developing a site on my local wamp stack. I have created an alias to view the site so i go to localhost/eee/ to view it. Ideally i would like to go to www.eee.lo but ever since upgrading to win8 I can't get it to work.
So this is the problem, i'm making modules for the website so i don't have to change all the code etc... And i don't want to have to go around changing all the url's when i migrate to the online server so i'm creating a file called _control.php which has this;
$_SITELOC = "localhost/eee/";
And then each time i want to include a file i will go;
include "$_SITELOC/scripts/inc/_header.php";
But this doesn't work and i can't work out why as if i echo it rather than include it and then i take what it prints and put it into the url it goes to the correct file. But it throws errors on the include, it gives two warnins;
Warning: include(localhost/eee/scripts/inc/_header.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'localhost/eee/scripts/inc/_header.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
I read somewhere that it might be to do with the include path so i tried;
set_include_path(get_include_path() . PATH_SEPARATOR . $_SITELOC."/scripts/inc/");
but this too did not work and now i'm not sure where to go.
Thanks, Chris
localhost/eee/ is your public address that you can use in your web browser. This public address should more appropriately be written as http://localhost/eee/. When you move to web server, you get the public address http://www.eee.lo/.
When including files, you have to use file paths. For example, if you have your www (or httpd, whatever) directry in D:\ on windows, then your include path should start with D:\www\eee\.
So, basically you have to use two variables to keep paths.
$_SITELOC = "http://localhost/eee/"; //For all URLs used in your HTML document.
$_INCPATH = "D:\www\eee\\"; //For all internal file includes.
In practice, you will need both of these, and it is good practice to keep the website address and internal paths out of your main script because when uploaded to remote server, not only your public address changes, but you will also have to deal with absolutely different internal (include) paths.
Your idea is basically good, to define one (root) path of the application and include files based on it, but unfortunately you're not doing it quite right. You have basically two ways of doing that.
One way (which I personally find better) is to include local files in your file system, where you can define the root path, i.e. like
define ('ROOT', 'your/document/root/path');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
The other way would be to include a web resource, what you're trying to do, but you've forgotten to specify the scheme (protocol) you want to use, i.e.
define ('ROOT', 'http://localhost/eee');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
For more information, see the examples, provided by the documentation for include
Note: If you want to include the source of a php file, i.e. file with definitions of functions, etc., use the first approach. Including files, using the second approach will only include the output produced by that file.
If you include() a URL, you will (probably) be including the output of the script's execution, when you want to include the script's source. It seems like you actually want to include by local file system path.
I am having a weird problem with my php. Whenever I try to move a file with rename(), not only the file is not moved, but also the directory to which it should be copied is deleted, together with all files within it. The original code is:
rename('temp.odt', 'tmp/report.odt');
but I have already tried other path delimiters like
rename('temp.odt', 'tmp\report.odt');
rename('temp.odt', 'tmp\\report.odt');
rename('temp.odt', 'tmp' . DIRECTORY_SEPARATOR . 'report.odt');
rename('C:\wamp\www\zaiko\temp.odt', 'C:\wamp\www\zaiko\tmp\report.odt');
all to no avail. The code comes from a 3rd-party module which is used in the system I am working on.
Points well checked:
The file 'temp.odt' does exist in the current directory;
The directory 'tmp' does exist and there are several files in it. Also it is not read only.
The target file does not already exist (the actual file name has a timestamp, I reduced it here for simplicity)
After running rename(), the 'temp.odt' file is intact in its original location, while the folder 'tmp' is vanished as well as everything inside it. The following warning is issued:
( ! ) Warning: rename(temp.odt,tmp\report.odt) [function.rename]: The system couldn't find the specified path*. (code: 3) in C:\wamp\www\zaiko\modules\mod_deliver.php on line 192
*translated from Portuguese
Running: Apache 2.2.17 with PHP 5.3.5 on Windows XP with NTFS
Editing:
Just found the cause of the problem. It turns out that the module used by the application uses, in turn, a compression library; this library uses a temporary folder with exactly the same name as the one used by the application.
It must use some sort of cache, which would explain why the error didn't appear 100% times.
Problem solved by changing the name of the 'tmp' folder to anything else.
Thank you all for your time, and sorry for bothering you with such a stupid thing that, as it turns out, had absolutely nothing to do with my initial guess and, consequently, with the question formulated.
The example on PHP.net tells you exactly what to do - use the ROOT PATH to the file - normally this can be got by using $_SERVER['DOCUMENT_ROOT'] (but this only goes to the htdocs/public_html directory - you need to specify the rest) or by manually typing the path in (but try to avoid this).
<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>
At a guess, the following should work (assuming this is your path) - this also checks that your file actually exists so it can be renamed - you need to make sure that tmp/ actually exists in the first place, but you will get an error popping out if it didn't:
<?php
$root = getcwd().DIRECTORY_SEPARATOR; // Obtain the current working dir
$srcpath = $root."temp.odt"; // The file you want to rename
$destpath = $root."tmp/report.odt"; // Where you want to rename the file to
// make sure file exists and its movable
if(is_writable($srcpath)){
// if it exists, rename it
rename($srcpath, $dstpath);
echo "File was renamed!";
} else {
echo "It seems that the specified file doesn't exist!";
}
?>
You were escaping characters by using backslashes - always use forward slashes (I know this is within a single quote, which is ok, but if you use double quote then you would wonder what's gone wrong)!