Jump to content

Recommended Posts

Im using a php code to create a form and load some txt.

Only problem is when i implement it into my html and css it goes funny, but works perfectly fine on its own.

<?php include("formtest.php"); ?>

 

<form action="processchange" method="post">
<textarea rows="10" cols="10" name="formtest">
<?php
$fn = "form.txt";
print htmlspecialchars(implode("",file($fn)));
?> 
</textarea><br>
<input type="submit" value=""> 
</form>

 

 

errors im getting

<br />
<b>Warning</b>:  file(test.txt) [<a href='function.file'>function.file</a>]: failed to open stream: No such file or directoryon line <b>5</b><br />
<br />
<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed on line <b>5</b><br />

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/
Share on other sites

I believe the path should be relative to the Main file, not the included file. So if you have

 

-root

--file1.php

--includes

---file2.php

--files

---data.txt

 

And file1.php is include'ing file2.php, with file2.php being the script that is accessing data.txt. The path would be 'files/data.txt'.

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-931459
Share on other sites

Yes, but the location of the script that's running the code is irrelevant, the important factor is where the top level script is. Assuming in your example change.php is the top level page then the relative path to data.txt is '../data/data.txt'. But that will only work if change.php is the top level script, not the file being included.

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-931484
Share on other sites

I wouldn't say it's an argument between relative and full paths...however you will almost always be better off declaring your paths with something like this...

 

define("APP_NAME", strtok($_SERVER['SCRIPT_NAME'],"/"));
define("APP_PATH", "/".APP_NAME);

 

This way no matter where your file is being called, you can always call your includes relative to the base URL.

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-931895
Share on other sites

<b>Warning</b>:  file(/data/data.txt)

 

im getting that error

 

i have root -> data -> data.txt

 

but my script is being run from

 

root -> login -> change.php

 

have you  tried

../data/data.txt ?

 

and absolute paths are fine. You can just define a constant in an include file and use that as the beginning of the absolute path, for example

define("MY_PATH", "path/to/root");

//to use
include(MY_PATH."/cookies.php");

 

that way you don't have to change every single include call. just change one line in the main include file

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-931899
Share on other sites

It's all very well saying that using define to create a constant is the best option. To a dgree your correct, but it doesn't work in all circumstances. In order to do that across the board you willl either have to define seperately in every page, which goes againt the point of using a constant in the first place, or you will have to put it in a file that you will then have to include on every page. What method do you use for the path to that file on the include?

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-931908
Share on other sites

well obviously you have to include it on every page, thats exactly what we said, but we never said that is the end all be all of solutions. obviously if that solution wouldn't work well for you, than don't do it. its a suggestion, not a command. However, in most sites I develop, I use a template system (the index.php?id=thispage kind), so that every one of my pages always has a config page included, or something like that (because the index page is basically the page that holds everything). There are also some $_SERVER variables that you can use the get the file path.

 

not to mention that having a magic string as an include in one place is a small sacrifice for not having to worry about your includes being wrong in my opinion. its not that much of a hassle to change one include. Oh and if you are using relative paths, all your includes are "magic" strings usually anyways.

 

However, you are right, having a defined constant and using absolute paths are not always the best way. As with all programming languages, there are always different ways to solve problems with different pros and cons

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-931917
Share on other sites

Haha, I wasn't saying it was a command.  I didn't think at any point that I came off saying that one way is law and anything else is wrong.

 

I find that a lot of beginners end up with a spiderweb of includes and I just wanted to push them in the right direction in terms of best practices.

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-931921
Share on other sites

Haha, I wasn't saying it was a command.  I didn't think at any point that I came off saying that one way is law and anything else is wrong.

 

I find that a lot of beginners end up with a spiderweb of includes and I just wanted to push them in the right direction in terms of best practices.

 

oh my reply wasn't directed at you.

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-931924
Share on other sites

My point was, if you can make at least the path to the include (your config file) relative, from that point on you can use the constant you defined. That way any move of server requires changing one line. Not one line of every page in your site. And yes, whilst relative paths are technically magic strings, as they are all relative to the same point they should never need changing.

 

Obviously theres always exceptions. I've changed technique many times. Mainly because development on localhost and then deployment can be a right pain in the arse, especially if the remote host is a shared host.

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-931930
Share on other sites

It's all very well saying that using define to create a constant is the best option. To a dgree your correct, but it doesn't work in all circumstances. In order to do that across the board you willl either have to define seperately in every page, which goes againt the point of using a constant in the first place, or you will have to put it in a file that you will then have to include on every page. What method do you use for the path to that file on the include?

 

You can set the includes path to point to the directory of this config (and on the way to your library of code) and from then on, just use the file name in the include command.

Link to comment
https://forums.phpfreaks.com/topic/176646-php-include-issues/#findComment-932446
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.