0

I am trying to run through a dictionary, .txt file, calculate the metaphone() values and append that to each line. Then write this to a new file.

I am getting an error though on the line that I am using fputcsv() and it says: expects parameter 1 to be resource, boolean given

I don't believe I am passing it a boolean. I don't understand what I am doing wrong.

<?php
$dict = fopen("originalDictionary.txt", "r");
$keyedDict = fopen("dictionary.txt", "w");
while ($line = fgets($dict)){
    $line = trim(strtolower($line));
    fputcsv($keyedDict, array($line,metaphone($line)));
}
fclose($dict);
fclose($keyedDict);
?>

Here is a link to originalDictionary.txt if that helps.

JayGatz
  • 271
  • 1
  • 8
  • 18
  • just responded to the comment... but it probably means that fopen('dictionary.txt', 'w') failed because of permissions. Crank up error reporting to E_ALL | E_STRICT and it should say as much. – Orangepill Jun 09 '13 at 18:20
  • @Orangepill Ah, okay. Do you think 755 is suitable permission for the directory to write dictionary.txt ? – JayGatz Jun 09 '13 at 18:36
  • @Orangepill I cranked up the error settings and that is the error I am getting. Actually since everyone needs to be able to write the file, well actually just me, wouldn't the permissions best be at 773? – JayGatz Jun 09 '13 at 18:40
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/14067623#14067623) – deceze Jun 09 '13 at 18:50

1 Answers1

0

fopen Returns a file pointer resource on success, or FALSE on error.

probably the opening of the file fails and the function returns false, there you have your boolean

check the file permissions and wether any errors are triggered (display them for example with ini_set('display_errors',1); error_reporting(E_ALL);

The Surrican
  • 29,118
  • 24
  • 122
  • 168
  • What should the permission be to write the file? 755? On the directory it is trying to write in? – JayGatz Jun 09 '13 at 18:36
  • that depends on the context. who is running the program? does the file already exist? then the file itself must have the right permissions. 755 is probably wrong, unless the owner of the directory is also the user under which the program runs. – The Surrican Jun 09 '13 at 18:38
  • @JayGatz Permissions as such are usually set at `0644`. But, in worse case, `0777` is a last resort. – Funk Forty Niner Jun 09 '13 at 18:41
  • No, the user and the owner are different. The user is running the programme. The file does not already exist. I think 773 is better because doesn't that grant everyone write access? Or is that too high? – JayGatz Jun 09 '13 at 18:41
  • @Fred I'm not comfortable doing 777. That is too high. I need the user and the owner to be able to write the file so shouldn't 773 be good enough? – JayGatz Jun 09 '13 at 18:42
  • the fist digit is for theowner, the second for group members, and the third for all others. in directories the execute bit is necessary to enter the directory. if you put the program user and the directory owner in the same group X7X should be fine. For example 775. the file will be created as the user who owns the program, so you should make it at least 66X. If you do not have them in the same group (the gorup must also be the files/directories group, you have to resort to the 3rd digit) – The Surrican Jun 09 '13 at 18:46
  • @JayGatz Either 773 ,or 775 as `The Surrican` stated. Give that a whirl. – Funk Forty Niner Jun 09 '13 at 18:52
  • @JayGatz Do you also want anyone who is not granted access to write, a `read` permission? – Funk Forty Niner Jun 09 '13 at 18:54
  • @TheSurrican 773 or 775 on the directory, right? Because the file doesn't exist yet. – JayGatz Jun 09 '13 at 19:07
  • yes, give that a try. the two users need to be in the same group and the group must be set for the directory. are you in a shared hosting environment? – The Surrican Jun 09 '13 at 19:09
  • Okay, it is fixed now. Thank you. – JayGatz Jun 09 '13 at 19:10