Jump to content

New file in different directory


WebCheez

Recommended Posts

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.)

 

 

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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/).

Link to comment
Share on other sites

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.

Link to comment
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.