TheJoey Posted October 6, 2009 Share Posted October 6, 2009 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 /> Quote Link to comment Share on other sites More sharing options...
itaym02 Posted October 6, 2009 Share Posted October 6, 2009 It searches the file in the directory of the main script (the one you put it's name in the browser). I guess that the file is not in that directory, but rather in the directory of the included file. I like to use absolute paths, and so does Accelerators. Quote Link to comment Share on other sites More sharing options...
TheJoey Posted October 6, 2009 Author Share Posted October 6, 2009 Absoulte paths? like full paths. Dont they do the same job. Quote Link to comment Share on other sites More sharing options...
TheJoey Posted October 6, 2009 Author Share Posted October 6, 2009 Like i said it works on its own but when i include it is when i get the problems <body> <p> Create form <?php include("formtest.php"); ?> </p> </body> Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 Is the included file in the same directory as the file it's being included in and is the file stored in $fn also in the same directory? Quote Link to comment Share on other sites More sharing options...
TheJoey Posted October 6, 2009 Author Share Posted October 6, 2009 Must of been how i was pointing to the file because when i move it all into the same folder and use just include('form.php'); it works fine.. thanks Quote Link to comment Share on other sites More sharing options...
TheJoey Posted October 6, 2009 Author Share Posted October 6, 2009 i had it as solved but i want it to read from a diferent directory and this isnt working $fn = "../stored/data.txt"; what can i do to replace this? Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 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'. Quote Link to comment Share on other sites More sharing options...
TheJoey Posted October 6, 2009 Author Share Posted October 6, 2009 <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 Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
itaym02 Posted October 6, 2009 Share Posted October 6, 2009 Oh, Come on, use full paths... Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 ... and have fun changing all those magic strings when you switch server. Quote Link to comment Share on other sites More sharing options...
nafetski Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 6, 2009 Share Posted October 6, 2009 <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 Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 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? Quote Link to comment Share on other sites More sharing options...
nafetski Posted October 6, 2009 Share Posted October 6, 2009 Every page I have has a link to my main include file, which defines those constants. Then I just have all of my URLs relative to that. Saves a lot of headache. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 6, 2009 Share Posted October 6, 2009 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 Quote Link to comment Share on other sites More sharing options...
nafetski Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 6, 2009 Share Posted October 6, 2009 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. oh, it seems we are arguing on the same side then Quote Link to comment Share on other sites More sharing options...
TheJoey Posted October 7, 2009 Author Share Posted October 7, 2009 mikesta... your a star. Thanks mate Quote Link to comment Share on other sites More sharing options...
itaym02 Posted October 7, 2009 Share Posted October 7, 2009 I started using Zend Framework a year ago. Have no such problems and almost no includes in my project. Using theire autoload and naming conventions. Quote Link to comment Share on other sites More sharing options...
itaym02 Posted October 7, 2009 Share Posted October 7, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.