Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to write a file to a different directory using fopen()

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

I've read that I cannot write a file by using the HTTP path, how do I use a local path?

$ourFileName = "http://www.xxxxxxxx.com/articles/".$thefile.".php";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
like image 525
user1142872 Avatar asked Mar 23 '12 10:03

user1142872


People also ask

Does fopen work on directories?

The fopen can't be used to create directories. This is because fopen function doesn't create or open folders, it only works with files. The above code creates a path to the file named 'filename'. The directory of the 'filename' is obtained using the 'dirname' function.

How do I specify a path in fopen?

in = fopen("path", " r ");

What does fopen () function do in PHP?

The fopen() function opens a file or URL. Note: When writing to a text file, be sure to use the correct line-ending character! Unix systems use \n, Windows systems use \r\n, and Macintosh systems use \r as the line ending character.


1 Answers

You should use the absolute or relative path to the file on the file system.

<?php

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

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

?>
like image 81
diolemo Avatar answered Nov 04 '22 21:11

diolemo