AL-zami
AL-zami

Reputation: 9066

file_put_contents fails to create file in the included path

I am trying to create a text file in the desktop.I created an included path using set_include_path() but the file is created in my xampp/htdocs folder.How can i create the folder in my desktop??Is it possible??

set_include_path(',;c:/users/shimantta/Desktop');

echo file_put_contents("/test.txt","Hello World. Testing!",FILE_USE_INCLUDE_PATH);

Upvotes: 4

Views: 8346

Answers (4)

Monzur Alam
Monzur Alam

Reputation: 558

For dynamically create robots.txt in the website root folder.

$data = "User-agent: *\nAllow: /wp-content/uploads/\nAllow: /wp-content/plugins/\nDisallow: /wp-admin/\n\n";
$data .= "Sitemap: {$site_url[ 'scheme' ]}://{$site_url[ 'host' ]}/sitemap_index.xml";

file_put_contents( $_SERVER['DOCUMENT_ROOT'] .'/robots.txt', $data, 0 );

Upvotes: -1

Adam
Adam

Reputation: 18807

Your "/" put it in the root folder of your installation

file_put_contents("test.txt","Hello World. Testing!");

will put it in the current directory of the current main page

file_put_contents(dirname(__FILE__)."/test.txt","Hello World. Testing!");

wil put it in the current script directory (if included).

If you want to put it in another directory, just tell the path

file_put_contents("/path/to/test.txt","Hello World. Testing!");

For windows, you might have to replace backslashes /path/to/test.txt with double backslashes \\path\\to\\test.txt

If you want to use FILE_USE_INCLUDE_PATH you have to remove the slash (which indicates an absolute path), and use the windows backslashes in your include path definition, specify your Desktop at first option. I think the file should exist before.

set_include_path("c:\\users\\shimantta\\Desktop;.");
echo file_put_contents("test.txt","Hello World. Testing!", FILE_USE_INCLUDE_PATH);

EDIT: you can know the path where the test.txt will be with the function stream_resolve_include_path("test.txt")

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

set_include_path does not specify the active directory, but the directory where to look in case you want to include a file. For instance:

set_include_path('C:\php\libs\');
include_once('lib.php');

What you are looking for is chdir that changes the active directory . So the following code will write your file to C:\documents\:

chdir('C:\documents\');
file_put_contents('test.txt',"Test");

Or you can specify the entire path yourself:

file_put_contents('C:\documents\test.txt',"Test");

Upvotes: 2

AbraCadaver
AbraCadaver

Reputation: 78994

The include path is the path that PHP searches when you include/require a file, not when you write to a file.

include_path Specifies a list of directories where the require, include, fopen(), file(), readfile() and file_get_contents() functions look for files.

Just give the complete path:

file_put_contents("c:/users/shimantta/Desktop/test.txt", "Hello World. Testing!");

This will only work if the user running the script or the user running the webserver has permission to write to that directory.

Upvotes: 4

Related Questions