WebCheez Posted April 26, 2011 Share Posted April 26, 2011 In my script, I'm trying to create a file with the name of a variable in a different directory. My code is: $handle = fopen("cheese/" . $page . ".html", "w"); Output: Warning: fopen(cheese/test.html) [function.fopen]: failed to open stream: No such file or directory in /home/a5938041/public_html/newfile/newpage.php on line 6 What's happening here? (sorry I'm a total noob.) Quote Link to comment https://forums.phpfreaks.com/topic/234797-new-file-in-different-directory/ Share on other sites More sharing options...
Zane Posted April 26, 2011 Share Posted April 26, 2011 You need "x" instead of "w" at the end If you want to write to this file, then use x+, though you really only need to do this once.. IMO it's better to just use UNIX's touch command..assuming you're running Linux PHP's touch() function. $newfile = `touch cheese/file.txt`; $newfile = touch("cheese/file.txt"); $handle = fopen("cheese/file.txt", "w"); Quote Link to comment https://forums.phpfreaks.com/topic/234797-new-file-in-different-directory/#findComment-1206659 Share on other sites More sharing options...
WebCheez Posted April 27, 2011 Author Share Posted April 27, 2011 woops, I also forgot: the directory cheese is not in the directory of the script. It's in the public_html directory. Should I try to create it in www.mysite.com/cheese/ ? Quote Link to comment https://forums.phpfreaks.com/topic/234797-new-file-in-different-directory/#findComment-1206685 Share on other sites More sharing options...
Fadion Posted April 27, 2011 Share Posted April 27, 2011 You can just use a path relative to the script's location: Script Location: /public_html/newfile/ File Location: /public_html/cheese/ <?php $h = fopen("../cheese/$page.html", 'w'); ?> ../ => moves one directory below the current one (/newfile/). Quote Link to comment https://forums.phpfreaks.com/topic/234797-new-file-in-different-directory/#findComment-1206689 Share on other sites More sharing options...
Zane Posted April 27, 2011 Share Posted April 27, 2011 touch should create that directory for you.. I believe and you don't need to go all the way back to public_html, just enter a relative location.. if you're already in public_html, then you should only need "cheese/txt.txt" as your argument. and yes.. GuiltyGear is right ../ => moves one directory below the current one (/newfile/). So you would need touch("../cheese/text.txt"); if you wanted to create it in a directory other than the one you are in. The only problem I could foresee would be permission issues. Quote Link to comment https://forums.phpfreaks.com/topic/234797-new-file-in-different-directory/#findComment-1206690 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.