$file=file('DATA.txt');
I keep getting "failed to open stream. No such file or directory in ...". This works when I run it on a server and browser. But the error occurs when I run the script using command line.
First of all take a deep breath and a tea ;)
The news is: DATA.txt is only a file-name.
A file normally is placed in a directory. As stupid as this sounds, well, as stupid this is: What is the directory?
The answer: the current working directory. This differs between servers and CLI.
So better add your directory as well:
$name = 'DATA.txt';
$dir = 'C:/';
$path = $dir.$name;
$file = file($path);
Hope this is helpful.
The question definitely lacks some clarifications, but two guesses:
Since you haven't provided a path, the file is expected to be located in the current dir, but server most likely runs as a different user (web, for example), and expects this file to be located in this user home dir (or current dir, to be precise).
Permissions, as usual.
I found that this works: $fname=str_replace(basename(__FILE__),'',__FILE__).'DATA3.txt';
I couldn't simply use "dirname" because I wanted this to work in both Windows and Linux (\ or /).
Related
I have set up a cron to call a php script to run some download tasks at a regular interval.
The website is hosted at Bluehost. I have followed the instructions on how to set up a cron and that basically works fine, but the behaviour is different from when calling the script manually which i suspect has to do with directory settings. When using the cron i get errors:
Warning: copy(wp-content/uploads/feeds/full/1.csv): failed to open stream: No such file or directory in /home1/user1/public_html/import/custom-downloader.php on line 86
my php code is copy( $row["externalURL"] , 'wp-content/uploads/feeds/full/'. $localfilename );
I have also tried below with same result.
copy( $row["externalURL"] , $_SERVER['DOCUMENT_ROOT'].'wp-content/uploads/feeds/full/'. $localfilename );
I think think that i need to construct the path to /home1/user1/public_html/import/wp-content/uploads/feeds/full/ but i don't see how to do that.
First thing's first, make sure that 1.csv exists and that it can be read (there's read permissions on the file)
Second thing, double check if 'wp-content/uploads/feeds/full/1.csv' really is what you want to copy. Your error message indicates that the php script calling the copy method is /home1/user1/public_html/import/custom-downloader.php. However, the path you specified is relative. So your PHP script (barring any changes to the PATH to look for other directories) will look for /home1/user1/public_html/import/wp-content/uploads/feeds/full/1.csv -- which may not be what you intended!
This can be solved by using an absolute path to your wp-content folder, or by moving this script to the parent directory of wp-content.
I've been on this bug for too long so I'm looking for some help from more experienced server programmers.
I'm running a Bitnami LAMP stack. I'm trying to use a PHP script (maintenance.php) to move files on the server. So I'm using rename(filepath, newpath) in my script. However, the PHP script cannot find the file even though it exists on the server.
SOLVED: The problem was that I was calling the script from two different places during debug (my browser, and my linux shell). The "working directory" in each of these places was different so the filepaths represented different locations depending on where I called the PHP script from.
There is no pictures directory at root so /pictures wont work.
Instead you will have to use absolute path to the pictures directory.
There are multiple ways to do this.
You can use $_SERVER['DOCUMENT_ROOT'] which will give you root directory under which the current script is executing.
reserved.variables.server
Also, you can use __FILE__ which will give you the path to current file.
With dirname(__FILE__) you can get directory and then you can work up to the pictures directory by going level down using ../.
This is also a similar question that you can check,
PHP include absolute path
I have seen several similar questions, but no answer worked in my situation, except that it probably has something to do with permissions.
A PHP script served by Apache tells me unable to open database file.
When I print the path to that file, it returns a valid path, say DBPATH. The file does exist at that location; I gave it and its parent folder 777 rights; I gave them user:user access, where user is the sudoer that all script files belong to. I did the same to the whole htdocs/ folder, just in case.
When I print file_exists(DBPATH), it returns false. Is is most likely a matter of permissions, but I don't know what I should change for PHP to have access rights. I tried apache:apache, too. I cannot su apache (user not available).
My scripts are in htdocs/. DBFILE is somewhere out of it (I tried /tmp/test, all in 777, but no luck either).
No safe_mode, PHP 5.4 freshly installed, CentOS7.
Please someone give me a clue at least to help debug it.
Maybe such as: how can I check whether my file will be readable from apache/my php script, without running the script itself? How can I get the name of the user that is used to execute it?
Solved, more or less.
To debug I had the idea to move DBFILE to the same folder where the PHP script lives, and check it can find it - it did. Then I move DBFILE one folder after another in the tree to see where it stopped finding it.
It occurs that if only one of the folders in the whole path does not have execute rights for all users (xx5), the file cannot be found and file_exists returns false.
So the solution was to create another folder in a totally executable place (/var/www/data/ worked after chmod 755 data), and move the file there.
Do you use an absolute path or relative path?
Because file_exists() doesn't work with HTTP addresses (which is an absolute path). But you can enter the relative path.
I had the same problem and it fixed it. It was the same problem with unlink().
Exemple:
$file_relative_path = "./wp-content/uploads/fileDirectory/fileName.jpg";
if (file_exists($file_relative_path)) {
unlink($file_relative_path);
}
I had a similar problem and was able to solve it by the answer of JulienD:
If the execute flag of a directory in the file system (Linux) is not set, then PHP (still) scans this directory with glob or scandir. However, a subsequent check with file_exists() on this list of results, I wanted to find broken symbolic links, returned false!
So the solution was to set the Execute right for the directory, as mentioned by JulienD.
The file was created by apache using mkdir() and fopen() etc..
How do i let my php CLI program be allowed to read that file that apache (that is what it says the user is) created so i dont get this error:
Warning: file_get_contents(./sessions/nl2larsjl6n3315mesghmspts7.txt): failed to open stream: No such file or directory in /var/www/html/cli.php on line 58
in the cli this my code for getting the file:
$alert = file_get_contents('./sessions/'.$sessionID.'.txt');
Short answer:
when you create it, you should run chmod(0777) on it.
Long answer:
chmod(0777) means "world readable and writable, and not actually recommended. Checkout http://en.wikipedia.org/wiki/Filesystem_permissions, http://www.linuxforums.org/articles/file-permissions_94.html for a complete explanation on these numbers.
Basically if you need it to be writable by a user, and readable by any user, do a chmod(0644)
EDit: i was quick to wrinte an answer... without reading the question carefully.
You seem to try and open the file from a different directory than the folder where you created it.
Try and see the result on echo getpwd() in the CLI script and then in the script running under www. I bet you will see different locations.
When executing CLI scripts you should either do a chdir() before running the command or use absolute paths at all times for the files you access.
I personally prefer the chdir() method.
I'm on a Windows machine. This seems like it should be unnecessary, but when I do it, everything suddenly works. Is there something wrong with my path? Do I need to add something to it to avoid having to copy DLLs?
Apache like any application will assume that the file is located in the same directory as the Current Directory path (check out http://en.wikipedia.org/wiki/Working_directory). If it's not there. The current working directory is USUALLY the same directory that httpd.exe (main executable) is in but it can actually be different if you do something like
C:\Apache2>bin\httpd.exe
In this case the Current Working directory is C:\Apache2 rather than C:\Apache2\bin.
If if the file isn't found there the application will naturally traverse the PATH environment variable. The PATH environment variable is a semi-colon or comma separate list of paths) to find the file.
Start -> Run -> Type "cmd.exe" and then in the Command Prompt type "echo %PATH%" to see the current path you have.
Finally, if the file wasn't found it will just error out.
As a tip you can actually track what files an application is trying to load and where they load them from by using Process Monitor. http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx
I've used this tool to solve load DLL problems in Apache before and other applications as well. Just simply add a filter for the app you are running and have it only sniff out file reads.
I donot know the internals of MySQL and apache.
My thought is this. Internal of your application is using libmysql.dll. And it seems that path is not proper so it searches in PATH environmental variable. apache/bin will be there in PATH directory. So it is taking the dll from this path. If the dll is not present in that path I think it fails to load and hence fails.
EDIT: Added the solutions which were added in comments
Try rebooting your machine. I had the same issue with mysqlpp library. Path was pointing to mysql bin dir but it still couldnt find libmysql.dll – Daniel (Jan 26 at 6:55)
Apache might be running with credentials different from your own (almost certainly so if you're running it as a service.) Try placing the dirs in the SYSTEM path, not the USER path. – moocha (18 hours ago)