Help with windows path - PHP - php

I have this path and it is correct however, the browser will not include the source file unless I put "file:///" in front of it. I'm still developing and this will ultimately be on a Linux machine but in the mean time, I'd like to see my work as well as be able to troubleshoot it. Is there a solution for this?
This fails:
C:\Program Files (x86)\work\site\js\rowlock.js
This does not fail:
file:///C:\Program Files (x86)\work\site\js\rowlock.js

Try using variable $_SERVER['DOCUMENT_ROOT'] to make your script independent. For example:
include($_SERVER['DOCUMENT_ROOT'].'/js/rowlock.js');
Works fine on any system

just use front slashes everywhere if you'll be moving this to a linux box anyway. php for windows can understand it.
$file='c:/Program Files (x86)/work/site/js/rowlock.js';

Put quotes around your path. You have spaces, so it is not read correctly.
'C:\Program Files (x86)\work\site\js\rowlock.js'

Where is Your root folder?
If its
C:\Program Files (x86)\work\site\
Then simple access your file like this
js/rowlock.js
This assuming that js is in the Root folder

Related

images and css not loading on local wamp server using PHP

I have ran into a problem where by using /js/filename.js or /images/image.jpg etc is not loading anything on my WAMP Server. What it seems like is that its not getting the base url properly here. Screenshots will give you better idea.
Trying to reference the files with "/" in the start but it fails to load anything. I need to have it because i would be working with URL Rewrites so in that case it should load the files from there. Please let me know about any wamp server setting that is causing this issue.
Regards.
You are referring to your files with a root-path like "/my-file.txt" (i.e. having a slash at begin),
which would result to "C:/wamp/www/my-file.txt" being requested.
Change all your paths to be relative paths, for example to "./my-file.txt" path (i.e. with both dot and slash ./),
That would change the file request to something like "C:/wamp/www/my-project/public/my-file.txt" (if your HTML file is in public directory that is).
Actually I would not recommend you using relative paths like ./myfile.txt or something else, as it can get somewhat messy when you try MVC in your code.
Instead I would recommend you set up a virtual host on your wamp machne.
Here you can read more about setting up virtual host in wamp
https://john-dugan.com/wamp-vhost-setup/

including files with $_SERVER['DOCUMENT_ROOT'] on localhost

I just moved my site to my local computer(a mac) but the site is not able to load properly because the include files doesn't seem to be able to include properly.
say eg:
I have the following lines:
include($_SERVER['DOCUMENT_ROOT'].'/pages/includes/functions.php');
on my index.php but the files are not able to be included properly.
I tried to use the functions in the file but it doesn't seem to be able to run the functions properly as per online.
I tried echo-ing $_SERVER['DOCUMENT_ROOT'] but it shows: /Applications/MAMP/htdocs
I'm not sure if that is the full path or if there is anything else i need to add to the url to get my files to be properly included. Perhaps i'm missing something. Any help will be greatly appreciated.

php require_once path difference between windows and linux

I have a file located in my CakePHP root folder placed under a folder named cron. Path is:
c:/wamp/www/project/cron/daily.php
This file requires another file placed inside vendor folder of cake structure, like this:
require("/../vendors/phpMailer/class.phpmailer.php");
And i run this daily.php from task scheduler. This is the scenario in my development site.(Windows system). It works fine as expected. When i migrated the project to Ubuntu(production site), the require statement started causing issues; it cant find the required file. I made a small change there, like this:
require("../vendors/phpMailer/class.phpmailer.php"); <= removed the preceding slash
And it worked. So my doubt is, is there a difference in how parent directory notation work in widows and Linux? If so, how can i overcome this? Its not feasible to remove a slash every time I move the project from my development site(windows) to production site (Linux).
I tried this:
require("./../vendors/phpMailer/class.phpmailer.php");
It worked in linux. But gave "no such file directory" error in windows. It seems windows works only with:
require("/../vendors/phpMailer/class.phpmailer.php");
Solution
From #TWCrap's help problem was solved as follows:
require(dirname(__FILE__)."/../vendors/phpMailer/class.phpmailer.php");
It works in both windows and linux(* tears of joy *). But in windows it produces path as:
C:\wamp\www\project\cron/../vendors/phpMailer/class.phpmailer.php
This path looks ugly and i hope it wont cause probs in future!
-Thanks guys!
AS i remember, when you put 1 dot infront of the line, you start at the directory you are. So then the line must look like this:
require("./../vendors/phpMailer/class.phpmailer.php");
And that should work at windows and linux....
Do not use absolute paths if you really do not need so. It's safer and better to correctly set include_path so in case of move you just need to adjust one setting instead of digging thru whole project and all its files.
So my doubt is, is there a difference in how parent directory notation work in widows and Linux?
Paths starting with / (i.e. /foo/bar) are absolute paths as starting / indicates root folder. On Windows you got drive letter there.
I also suggest using require_once to avoid duplicated requires (which is OK if you mix HTML with code, but "spaghetti code" is not recommended anyway), but may cause problems with code
The problem is that, at least on a UNIX system, when you start with a preceding slash on a file path you start at the root.
You should either write ./../* or just ../ ond both systems. It should work both.

what is the difference of absolute path in html and php?

I am developing a website on php, I have installed wamp on my personal computer and my website files are in the www folder of wamp.
now considering www as my root folder i have a template folder in the root folder and header.inc.html file in the template folder. when I try to include this header.inc.html file in any other php file using an absolute path include('/template/header.inc.html'); it gives me error "Failed to open stream: No such file or directory", but when I create a simple html link using the same absolute path it works perfectly and opens the file. below is my test code
<?php
echo 'headerfile';
include('/template/header.inc.html');
?>
if I give the full path for example C:/wamp/www/template/header.inc.html to the include function it works fine.
I am confused that this problem is occurring on my wamp server only and it would work perfectly on any webhost server, or maybe the same problem will exist on a webhost
I would appreciate any help that would clarify my confusion, Thanks.
Absolute paths on the server start from the server's hard disk (C:\).
Absolute paths on the client start from the root of the website (http://example.com/).
You can make use of __DIR__ to make some file on disk relative to the php-file on disk itself:
include(__DIR__.'/template/header.inc.html');
This should solve your issue.
The difference is not that easy to explain because both types of paths - even related - are two pair of shoes. I suggest you start with a very basic HTML website tutorial that explains how to link on your website and where files are located and how that is related to the webserver configuration.
HTML pages live in the client's browser that know nothing about your server's folder structure, and they're relative to the domain name eg. http://example.com/.
PHP programs run on the server side and they deal with the server folders. You shouldn't hardcode full paths in your php programs, because it will cause problems whenever you'll move them between the development server and the live host (just to name an example). Therefore in php files you should either use relative paths to your file, or use the __DIR__ magic constant that gets substituted with the directory where the php file is.
1.) First approach: include('template/header.inc.html');
2.) Second approach: include(__DIR__ .'/template/header.inc.html');
In your case (working on a development machine) both the client and the server is the same box, that might be confusing you.

How did my website break after deploying it?

Why did all my require_once calls that are written with paths like ../.../ this folder/thifile.php broke as soon as I deployed my website?
Are they wrong? On the localhost they were working just fine.
I got his errorfailed to open stream: No such file or directory in , and my file structure has not changed after I deployed it
Are you sure the paths point to the same places? If you have a different directory structure on your deployed site, relative paths like "../../etc" may not point to the same files.
I didn't bring my chrystal ball, so you might need to explain some more, but
You host has a different system than you (windows vs linux?), so the / and \ are not compatible?
There was some other problem (hard to guess, since you didn't provide an error)
Not enough rights to read the files
A virtual directory, so you can't go back in the structure like that, cause you're getting a different (real) dir?
Its definitely a path issue. Try going to the files u included using http://yourdomain/path/you/included

Categories