require path in php - php

I have the following code:
require("\create_form\view.php");
and receiving the following error:
Warning: require(\create_formiew.php) [function.require]: failed to open stream: Invalid argument in C:\xampp\htdocs\training\school\STU001_MAIN.php on line 67
You can see from the error message that "require(\create_formiew.php)" has a missing "\v", whereas I wrote "\create_form\view.php".
What's the problem?

Either escape the backslashes in double quotes, or just use forward slashes. Forward slashes work on Windows too.
Also you probably don't want to use an absolute path.
require("./create_form/view.php");
Or make it relative to the document root with:
require("$_SERVER[DOCUMENT_ROOT]/create_form/view.php");
// Note: Use without key quotes only in double quoted string context!

You can use 'DIRECTORY_SEPARATOR' to avoid errors with system specific file paths.

\v is the escape sequence for a vertical tab.
Either use single quotes in your strings or escape the backslashes (\\) to avoid ambiguities.

try using
require("/create_form/view.php");

Try using double (\) backslashes between directorys:
require("\create_form\view.php");

Related

PHP glob can't read tmp

I am using wamp in win7, and I try to read c:\wamp\tmp by glob with php, but the result is an empty array
print_r(glob("C:\wamp\tmp\*"));
I can see any folder in c:\wamp except c:\wamp\tmp, and I make sure that folders has the same setting about read write...
\t is an escape sequence that means the Tab character. You have several ways to deal with it:
Escape it with another backslash.
print_r(glob("C:\wamp\\tmp\*"));
Use single quotes instead of double quotes, since escape sequences aren't processed in single quotes.
print_r(glob('C:\wamp\tmp\*'));
Use forward slashes instead of backslashes, since Windows allows either as a directory separator.
print_r(glob("C:/wamp/tmp/*"));

unlink() Function invalid argument

I have a file with directory: PDF\9783790820874-c1.pdf
I would like to delete this file with unlink() funciton. But it seems like not working if I set the directory into a variable and unlink it.
For example:
$FileToDelete = "PDF\9783790820874-c1.pdf";
unlink($FileToDelete);
The code is logic isn't it? but why when I execute it, it show me error message:
Warning: unlink(PDF\9783790820874-c1.pdf ): Invalid argument on line 36
I have to save file directory into a variable to work well with my program, is there any way to solve it?
Your backslash is being interpreted as an Escape Sequence.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Either change it to a forward slash (which does work for paths on Windows):
$FileToDelete = "PDF/9783790820874-c1.pdf";
Or use single quotes:
$FileToDelete = 'PDF\9783790820874-c1.pdf';
Or just escape the backslash:
$FileToDelete = "PDF\\9783790820874-c1.pdf";
I presume you are on Windows based on the backslash path separator.
I think the problem is due to escaping backslash causing problems, try:
$FileToDelete = "PDF\\9783790820874-c1.pdf";
unlink($FileToDelete);
Hopefully that will fix it for you.
Remember that '' and "" behave differently in relation to escaping within the string. So while 'a\b.pdf' might work "a\b.pdf" could mean something completely different (where a and b are [some string]).

How Do I Insert a Variable into a PHP 'Include'?

Not much to explain really, I just cant figure out the correct syntax to use a variable in an include URL
heres what I thought it would be:
$round_num=1;
include '../../fixtures/round{$round_num}fix.php';
It returns the usual:
Warning: include(../../fixtures/round{$round_num}fix.php) [function.include]: failed to open stream: No such file or directory
This must be a very simple one for allot of you out there lol but i just cant find my answer ANYWHERE!
Use double quotes (") to delimit your string. String interpolation doesn't occur in single quoted (') strings.
$round_num=1;
include "../../fixtures/round{$round_num}fix.php";
you can also use alternate syntax such as:
include "../../fixtures/round" . $round_num . "fix.php";
Sometimes people prefer to have the variable highlighted by their IDE for clarity.

Accessing a defined constant returns messed up characters

This is strange I have a constant defined as such:
define("RJ_FILES_PATH", RJ_SITE_DIRECTORY."\assets\files\\");
However when I try to access the constant in code I get this weird result on my localhost..
C:\wamp\www\my.app\assetsiles\2688
The \f is replaced by a square indicating it as an unrecognised character. Whats happening because of this I am unable to save files to the folder the constant is supposed to point to.
You need to escape the backslashes before the a and the f:
define("RJ_FILES_PATH", RJ_SITE_DIRECTORY."\\assets\\files\\");
Otherwise they're interpreted as escape codes, since they're within a string. (Just like \n is a newline, et cetera.)
You could—and probably should—just use forward slashes (/) in your file/directory paths. PHP will automatically convert them to the value of the built-in system-dependent constant DIRECTORY_SEPARATOR when using the string as a file path. This is by far the most cross-platform method of doing it.
Alternatively, you could use single quotes. They interpolate backslashes (\) differently in that most escapes are ignored and just interpreted literally (the exceptions being \\ and \').
# *
define('RJ_FILES_PATH', RJ_SITE_DIRECTORY.'\assets\files\\');
# * still need an escape here because of \'
You should escape the backlash by double it: \
In my opinion, you always should use '/', because it work fine in windows and linux. In php, there's a constant DIRECTORY_SEPARATOR but it's uneccesary because '/' work fine.

PHP replace double backslashes "\\" to a single backslash "\"

Okay so im working on this php image upload system but for some reason internet explorer turns my basepath into the same path, but with double backslashes instead of one; ie:
C:\\Documents and Settings\\kasper\\Bureaublad\\24.jpg
This needs to become C:\Documents and Settings\kasper\Bureaublad\24.jpg.
Note that you may be running into PHP's Magic Quotes "feature" where incoming backslashes are turned to \\.
See
http://us2.php.net/magic_quotes
Use the stripslashes function.
That should make them all single slashes.
Have you considered the stripslashes() function?
http://www.php.net/stripslashes

Categories