Reputation: 1200
I have a password protected directory I want to write to a text file in PHP. cURL
is probably what it'll take, but I'm trying fopen
right now:
$filename = "example.txt";
$file = "https://user:[email protected]/pass-protected-folder/" . $filename;
$fh = fopen( $file, 'w' );
fwrite( $fh, "Testing 1, 2, 3.." );
fclose( $fh );
Which does not work; no errors but no file gets written either.
What should I do to make fopen
work and/or if cURL
is a better option?
Upvotes: 0
Views: 1693
Reputation: 5921
The HTTP and HTTPS Protocol won't let you create Files, there are PUT and DELETE but they rely on an Implementation.
The Password Protection is only for Requests through the Webserver.
If the File is located on the same server, you could use the realtive or absolute path to the file.
For Files on an external Server you could use ssh or scp for that (since, i guess apache, wont let you write over http/s) : libssh extension (php.net/manual/en/ref.ssh2.php) or in php `phpseclib' ( phpseclib.sourceforge.net ). If you don't mind security, you could also use FTP.
libssh Example:
$connection = ssh2_connect($myssh['host'], $myssh['port']);
ssh2_auth_password(
$connection,
$myssh['username'],
$myssh['password']
);
$localfile = $myssh['localpath'].$file;
$remotefile = $myssh['remotepath'].$file;
ssh2_scp_send(
$connection,
$localfile,
$remotefile, 0644
);
Upvotes: 1