closser
closser

Reputation: 57

Create a file and save it to a parent directory using PHP?

How can i Create a file and save it to a parent directory?

So as you can see from my script below, the PHP saves the "referer.log" file to the same directory where this PHP is, but i want it to save to a parent directory?

This is my Current PHP:

<?php
$fname="referer.log";
$file=fopen($fname,'a+');
$url = "$_SERVER[REQUEST_URI]";
fwrite($file,"

Referer: $url

");
fclose($file);
?>

Lets say my current directory is

/forum/link/contact/

i want it to be saved on

/forum/link/logs/

Thanks everyone.

Upvotes: 0

Views: 2924

Answers (2)

Janaka
Janaka

Reputation: 96

Option 1 Relative path $fname="../logs/referer.log";

Option 2 Absolute path dirname( dirname(FILE) )."/logs/referer.log";

Upvotes: 0

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

You are free to use relative path:

$fname = "../logs/referer.log";

I would suggest you to check whether the directory exists and create it if it’s not:

if( !is_dir('../logs') ) {
  mkdir( '../logs', 0750, true );
}

Hope it helps.

Upvotes: 4

Related Questions