Re: fopen() with permissions?
| From: | Ernest E Vogelsinger | Date: | Wed, 06 Nov 2002 22:42:44 +0000 |
| Subject: | Re: fopen() with permissions? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-123080@lists.php.net to get a copy of this message | ||
At 22:48 06.11.2002, Charles Wiltgen said:
--------------------[snip]--------------------
>Well, I'm able to make an empty file with
>
>
> fopen("ftp://username:password@domain.com/folder/file.prefs",
> "w")
>
>However, I'm still nowhere because this file belongs to the user and her
>group, not to Apache, so I still can't write to it.
--------------------[snip]--------------------
Why don't you do it this way:
$fp = fopen("ftp://username:password@domain.com/folder/file.prefs",
"w");
if ($fp) {
// now write whatever you want to
fwrite($fp, 'Some content', 12);
fclose($fp);
}
else die('Can\'t create target file');
I don't see a reason why you shouldn't directly use the file handle
returned by fopen - that's why the PHP god has created it :)
See this sample code how to achieve this (substitute any necessary data).
Note that when opening a file using the FTP protocol you cannot
truncate/replace an existing file, you need to delete it first (this is
dictated by the FTP protocol AFAIK).
<?php
// --- configure this
$user = '**user**';
$pass = '**pass**';
$ftphost = 'ftp_upload_host';
$wwwhost = 'webhost_to_test';
$ftpfile = '/www/sample.html';
$wwwfile = '/sample.html';
// -- end configure
$time = strftime('%D %T');
$link = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
$x = <<<EOF
<html>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache" content="none">
<title>**By PHP** $wwwhost$wwwfile **By PHP**</title>
</head>
<body>
<h1>Created by PHP via FTP</h1>
Creation time: $time<p>
<a href="$link">Test</a>
</body>
</html>
EOF;
error_reporting(E_ALL);
$fp = ftp_connect("$ftphost");
if ($fp) {
$login = ftp_login ($fp, $user, $pass);
ftp_delete ($fp, $ftpfile);
ftp_quit($fp);
}
else die('Cannot connect to FTP');
$fp = fopen("ftp://$user:$pass@$ftphost$ftpfile",
'w');
if ($fp) {
fwrite($fp, $x, strlen($x));
fclose($fp);
header("Location: http://$wwwhost$wwwfile");
}
else die('Cannot create file via FTP');
?>
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/