file_put_contents() file owner in OSX

My php script is running with owner “denny” as reported by

get_current_user()

but the files it creates with

file_put_contents()

show the owner of the files as “www” as reported by Get Info in Finder.

chown()

does let let me change the owner: “Warning: chown() [function.chown]: Operation not permitted in …”

How do I tell php to create the file with owner “denny”?

The owner of the PHP Process is ‘www’ (you can verify this with the ps command a the command-line).
If it is important for ‘denny’ to have access to the files (although, unless ‘denny’ is an administrator of the machine, you should let your PHP application control all files and access to them for each user) and that is a user on the system, you can add ‘denny’ and ‘www’ to the same group.

I believe if you provide a little more detail about what you are trying to accomplish (the “Why” behind this) there are many here who can point you to success.

get_current_user is a bit of a misnomer - I think you’ll find that it gets ‘denny’ because thats the owner of the php file currently in execution. However, the actual user executing the PHP script is ‘www’.
You’ll need to chown, so www will need sudo capability.

Is this for a local machine, or are you trying to do this on an actual deployment?

It’s a little word processor web app. I write the text with BBEdit

<!-- text starts here -->
<br><br>
<h1>Mantenimiento de Desagües</h1>
<br>
<p>Estimados Copropietarios:</p>
<p>Para minimizar los inconvenientes con los desagües de los apartamentos la Junta de Condominio ha recopilado las siguientes sugerencias.</p>
<h2>1.- Uso apropiado</h2>

and the script displays the final letter on the screen and saves the completed letter to a folder.

It works fine but the file is saved with owner “www” meaning it can’t be edited in BBEdit until I change the owner which I can do but it’s a PITA.

This is running on my laptop. On my server I have an issue with permissions that I need to resolve with my web host.

For now it’s on my laptop. What do I “chown?” (yes I have sudo capability).

On my server I have an issue with permissions that I need to resolve with my web host.

Correction, on the server there is no problem with the permissions but I do have a problem converting diacritics to html (accents, etc. –> ¡¿áüç <– characters we use in Spanish)

On your laptop, if -www- has sudo capability, you can drop him to the command line and [FPHP]exec[/FPHP] the chown.

What are you trying to convert that doesnt get converted by [FPHP]htmlentities[/FPHP]?

On the contrary, it converts too much! Like “<” and “>” which ruins the html! :eek:

Some functions have an exclusion or inclusion parameter but not htmlentities() that I know of.

So when you [FPHP]htmlspecialchars_decode[/FPHP] the result on the other end, it comes out wrong?

I don’t use [FPHP]htmlspecialchars_decode[/FPHP]. I need to explain this hack.

I believe that in a ideal world we should only have three desktop apps: a browser, an ftp package, and a text editor. Everything else should be a web app. Since PCs are now servers, we can use the web apps locally which is what I’m doing with the one I’m testing now.

My web app (which does not yet have a name) creates an HTML5 document which prints a letter just like a word processor document using a template for the letterhead and a text document for the letter body. In fact, the browser acts like a word processor. It’s a simple script really:

<?php
 
$files = "/Users/username/Sites/path/to/letters/";

// this is the html header and the letterhead
$html = <<< AVILAAVILA
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Edifico Avila</title>
<link rel="stylesheet" type="text/css" href="avila.css">
</head>
<body>
<div class="logo">
<span>edif.<span>A</span>vila</span><br>
Avenida Buenos Aires, Los Caobos, Caracas.
</div>

AVILAAVILA;

// this is the letter's body
$html .= file_get_contents("+avila.html");
$html .= "</body>\
</html>\
";

// create a ubique file name
$date = date ("m-d-y");
$filename = "{$files}avila-{$date}.html";
$append = "";
while (file_exists ($filename)) {
    $append += 1;
    $filename = $filename = "{$files}avila-{$date}-{$append}.html";
    if ($append > 10) {
        die ("too many files already");
    }
}

// adjust some URLs for archiving
$html1 = str_replace ("avila.css", "../avila.css", $html);
$html1 = str_replace ('src="images/', 'src="../images/', $html1);
$html1 = str_replace ('<body>', '<body style="border:none;">', $html1);

// save the file
if (file_put_contents ($filename, $html1) === FALSE) {
    die ("Cannot write to file ($filename)");
}

// show some feedback on the screen
echo "<div class='no-print'>";
echo "Success, $filename";
echo "</div>";

// display the letter
echo $html;

?>

The problem is that in Spanish we use diacritical marks: accents, tildes, cedillas, and umlauts (áéíóúñçü) plus opening exclamation and question mark (¡¿). The best way to deal with them is to use the html entity codes. Typing the html entity codes is pita so I want to use some code to translate them. But htmlentities also converts the open and close html tags “<>” which I need to render the html correctly. I also have the option of using a BBEdit Text Factory but I prefer a php solution. I have a function for the conversion which works on my laptop but gives funny (UTF-8?) codes on my LAMP server.

function c2html($text) {

    $table = array(
    '¡' => '&iexcl;',
    '¢' => '&cent;',
    '£' => '&pound;',
    '©' => '&copy;',
    '®' => '&reg;',
    '°' => '&deg;',
    '¿' => '&iquest;',
    'Á' => '&Aacute;',
    'Ç' => '&Ccedil;',
    'É' => '&Eacute;',
    'Í' => '&Iacute;',
    'Ñ' => '&Ntilde;',
    'Ó' => '&Oacute;',
    'Ú' => '&Uacute;',
    'Ü' => '&Uuml;',
    'á' => '&aacute;',
    'ç' => '&ccedil;',
    'é' => '&eacute;',
    'í' => '&iacute;',
    'ñ' => '&ntilde;',
    'ó' => '&oacute;',
    'ú' => '&uacute;',
    'ü' => '&uuml;',
    '&' => '&amp;',
    '¥' => '&yen;');
    
    return strtr($text, $table);
 }

Thanks for listening! :wink:

You really dont need to explain it so much. You have some HTML you need to encode and store, and then decode only the html characters, leaving the diacretics.

[FPHP]htmlentities[/FPHP]
[FPHP]htmlspecialchars_decode[/FPHP]

Interesting idea! But I would have to change the workflow. Let me think about it and I’ll get back to you. Thanks! :slight_smile:

Brilliant!

$html .= htmlentities(file_get_contents("+avila.html"), ENT_NOQUOTES, "iso-8859-1");
$html  = htmlspecialchars_decode($html);
echo $html;

Thanks! :cool:

BTW, one has to make sure the file itself (+avila.html) is saved as “iso-8859-1” otherwise it does not work.