Attempting to write a file to another directory using the fopen () function

I am trying to write a file from one directory to another. For example, http://www.xxxxxxx.com/admin/upload.php at http://www.xxxxxxx.com/posts/filename.php

I read that I can not write the file using the HTTP path, how to use the local path?

$ourFileName = "http://www.xxxxxxxx.com/articles/".$thefile.".php";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
+5
source share
3 answers

You must use the absolute or relative path to the file system file.

<?php

$absolute_path = '/full/path/to/filename.php';
$relative_path = '../posts/filename.php';

// use one of $absolute_path or $relative_path in fopen()

?>
+8
source

You can open a file from a directory in the parent directory of this file using the relative path.

, /foo/x /foo/y ../x. , , , " ". , /foo/../foo/bar /foo/bar. , . hardcode - .

, /thefile.php admin/upload.php:

// path to admin/
$this_dir = dirname(__FILE__);

// admin parent dir path can be represented by admin/..
$parent_dir = realpath($this_dir . '/..');

// concatenate the target path from the parent dir path
$target_path = $parent_dir . '/articles/' . $theFile . '.php';

// open the file
$ourFileHandle = fopen($target_path, 'w') or die("can't open file");

paths.

+3

You can always access the local path view http://www.yourdomain.com/ with $ _SERVER ['DOCUMENT_ROOT'].

<?php
$f = fopen( $_SERVER['DOCUMENT_ROOT'] . '/posts/filename.php' );
?>
+2
source

All Articles