PHP - No such file or directory (LAMP server) - php

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

Related

php relative path fails

I have been setting up a new web server to replace an aging one and I am having some problems with relative paths that work on the existing server.
The existing server is Centos 6 with php 5.6, however the new server is Centos 7 with the same php version. I should also mention that both servers run WHM/cPanel with a single user (solo) licence.
I have several folders one level above the public_html folder that contain some include files and also some php files that are run from cron jobs. I put these here as there is never any need for them in the public_html folder.
// Example folder structure
-includes
-cjobs
-public_html
-includes
-css
-js
index.php
On the existing server, the following works ok on a file in the cjobs folder
include "../public_html/includes/mail_include.php";, however on the new server it fails. I have ended up using include_once (dirname(__FILE__) . "/../includes/mail_include.php"); and also had to relocate the cjobs folder into the public_html folder. Note that full paths do work ok, so /home/username/includes/mail_include.php does always work, but I don't want to hard code the paths.
I feel that there may be some small configuration setting in php that I have not changed, but I just have not been able to figure out why I need to include the files this way. It means I need to modify thousands of files just so they will work on the new server which is proving to be a real pain.
Any advice is appreciated.

Is there a way to get the document root on localhost in the same format that we get on remote servers (like /home/MyUserName/public_html)?

CodeIgniter documentations says:
For the best security, both the system and any application
folders should be placed above web root so that they are not directly
accessible via a browser. After moving them, open your main index.php
file and set the $system_folder and application_folder variables,
preferably with a full path, e.g. '/www/MyUser/system'.
and through echoing the PHP $_SERVER['DOCUMENT_ROOT'] variable I've gotten the document root of my remote server, which was /home/MyUserName/public_html but when I tried to do the same on my localhost I've gotten a different type of pathes (a windows path), it was D:/xampp/htdocs. Why? and Is there a way through which I can get a path like the first one on my localhost?
It's always preferable to develop/test on the target platform (Linux here), but you can have a look at something like http://cygwin.com/ if you want to stay developing on Windows. It will get you closer.
The first path is a unix file system path - the second a windows path. It isn't necessary to have an unix style path on windows. The only thing you have to do is place codeigniter in a path, which is not accessible from outside - for example D:/xampp/some_other_path

How to access a shared file from multiple sites on the same server

I have a dedicated server with a fair few sites running. I have a file I'd like to share from one site to all of the others using a php include. I know URL includes are turned off for good reason so is there any secure way to share the file?
i.e. site1 has the file at http://www.site.com/shared_file/share.php
I want to be able to access it from Sites 2,3,4,5 etc using
<?php include ("/home/site/public_html/shared_file/share.php");?>
Any ideas? At the moment I get a permission denied error. The server is running WHM and CPanel (latest versions).
Thanks
Brett
usually stuff like this is handled through include path.
Create special place for you files e.g. /libs/php/
Put your files in there
Add include path in your php script set_include_path(get_include_path(). PATH_SEPARATOR . '/libs/php/');
Simply to require_once or include_once or include with relative path to that location
For example if your file is located in /libs/php/shared/share.php you need to use require_onceshared/share.php`
If you have direct access to the server shell, how about using symlinks?
ln -s source dest
(assuming it's a Linux server).

Environmental variable not working (Win 7) for a batch file (cake.bat from CakePHP)

I'm getting started with CakePHP, and I'm trying to set my Windows 7's (64-bit) environmental variable PATH to recognize C:\xampp\htdocs\cakeblog\cake\console\cake.bat anywhere when using the command prompt.
I have set the environmental PATH variable for php.exe, and it is working great. However, when I was setting it, it didn't work when I added the path to the system variable. I had to add it to the user variable for it to work for some reason. I have no clue why this is so.
I tried doing the same for "C:\xampp\htdocs\cakeblog\cake\console," but it doesn't work for some reason neither in the system or user variable. I'm restarting the command prompt for the change broadcast to take effect, but it still doesn't work, while it works for php.exe. are .bat files different handled or something? I can't start the CakePHP baking process!
Any idea how to get this path included so that I can access cake.bat? Creating another .bat file (like runcake.bat) to directly run C:\xampp\htdocs\cakeblog\cake\console\cake.bat is not an option, as cake.bat creates files in the folder relative to where it's run. So, if I go to C:\xampp\htdocs\cakeblog\cake\console\ and run cake.bat, it will try to create files as C:\xampp\htdocs\cakeblog\cake\console as its root directory when I really want the files to be created somewhere like C:\mysite.. TIA!
My user path is below. cake.bat is located in the second directory.
C:\xampp\php; C:\xampp\htdocs\cakeblog\cake\console
UPDATE:
I just gave up with this approach and just wrote a short batch file named setpaths.bat that includes "set path=C:\xampp\htdocs\cakeblog\cake\console;C:\xampp\php" in it. This seems to override my user path variable and works great. I'll just have to run setpaths.bat every time I need to bake something.
A space after the semi-colon was the problem in my case.
C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program
Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program
Files (x86)\NVIDIA
Corporation\PhysX\Common;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program
Files\Intel\DMIX; K:\Xampp\php;K:\Xampp\cake\cake\console;
C:\Program Files\TortoiseSVN\bin;C:\Program
Files\TortoiseGit\bin;C:\Program Files (x86)\Git\cmd;C:\Program Files
(x86)\Windows Live\Shared;C:\Program Files (x86)\QuickTime\QTSystem\
Make sure it's in the Path variable, try removing any spaces between semicolon and the next path. Just a wild guess.
My Windows 7 environmental path variable is not working correctly for some reason. I have done the same thing on my home computer (same OS), and that one works just fine. I just gave up with this approach on my work computer and just wrote a short batch file named setpaths.bat that includes "set path=C:\xampp\htdocs\cakeblog\cake\console;C:\xampp\php" in it. This seems to override my user path variable and works great. The .bat file needs to be run during each session though. Hope this helps someone else who might be having a similar problem.

Why do I have to copy the libmysql.dll to the apache/bin directory to get the PHP extension to load properly?

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)

Categories