Page 1 of 1
fopen() won't work
Posted: Thu Jun 10, 2004 3:11 am
by Chris Corbyn
Hi,
I just set up IIS on my brother's PC and PHP 4.3.7. I just wrote a little script that keeps a text file with a list of films he owns but I can't get the fopen command to work so that he can use PHP to write new film names the the file.
I just used the fwrite script in the manual and whatever file I choose to open for writing I get the message:
Cannot open file (D:\\movies.txt)
The file permissions are not read only or anything like that.
Any Ideas?
Thanks

Posted: Thu Jun 10, 2004 3:20 am
by markl999
Got the full error message and the code that causes it?
Posted: Thu Jun 10, 2004 3:25 am
by feyd
try fopen("d:/movies.txt","wb");
Posted: Thu Jun 10, 2004 3:27 am
by Chris Corbyn
Code: Select all
<form style="margin:0px" method="post" action="index.php">
<table bgcolor=white width=50%>
<tr>
<td>
<font color=blue>
<b>
Text file location:
</b>
</td>
<td>
<input type="file" name="loc" size=40>
</font>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
Movie Name:
</td>
<td>
<input type="text" name="movname" size=60>
</td>
</tr>
<tr>
<td>
Category:
</td>
<td>
<select name="movcat">
<option name="series">Series</option>
<option name="film">Film</option>
<option name="mini">Mini-Series</option>
<option name="oneoff">One-Off</option>
<option name="catother">Other</option>
</select>
</td>
</tr>
<tr>
<td>
Other, Please Specify:
</td>
<td>
<input type="text" name="catotherspec">
</td>
</tr>
<tr>
<td>
No. of Discs:
</td>
<td>
<select name="discs">
<option name="cd1">1</option>
<option name="cd2">2</option>
<option name="cd3">3</option>
<option name="cd4">4</option>
<option name="cd5">5</option>
</select>
</td>
</tr>
<tr>
<td>
Quality:
</td>
<td>
<select name="qual">
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
<option name="4">4</option>
<option name="5">5</option>
<option name="6">6</option>
<option name="7">7</option>
<option name="8">8</option>
<option name="9">9</option>
<option name="10">10</option>
</select>
</td>
</tr>
<tr>
<td>
Format:
</td>
<td>
Video:
<select name="vidformat">
<option name="XVid">XVid</option>
<option name="DivX">DivX</option>
<option name="MPEG">MPEG</option>
<option name="vidother">Other</option>
</select>
Sound:
<select name="audformat">
<option name="AC3">AC3</option>
<option name="MP3">MP3</option>
<option name="audother">Other</option>
</select>
</td>
</tr>
<tr>
<td align=center colspan="2">
<input type="submit" name="submit" value="Update">
</td>
</tr>
</table>
</form>
<?php
$filename = $_POST['loc'];
$somecontent = $_POST['movname']."\r\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
It allows him to specify the location of the text file to keep the list in, then enter some fields and write to the file.
I don't get an error message as such, it just prints the line that it should when fopen fails.
Thanks in advance
Posted: Thu Jun 10, 2004 3:43 am
by feyd
on windows machines you need either a t or b added to the end of the fopen call.. i.e. fopen("d:/movies.txt","ab");
Posted: Thu Jun 10, 2004 6:58 am
by Chris Corbyn
I've tried adding both t and b but neither will work.
I wonder if it's got something to do with php.ini?
Posted: Thu Jun 10, 2004 7:31 am
by Chris Corbyn
Just enabled E_WARNING errors to see what it shows and it says...
Warning: fopen(D:\\test.txt): failed to open stream: Permission denied in D:\Web Pages\IIS\films\index.php on line 146
Warning: fwrite(): supplied argument is not a valid stream resource in D:\Web Pages\IIS\films\index.php on line 147
Warning: fclose(): supplied argument is not a valid stream resource in D:\Web Pages\IIS\films\index.php on line 148
Permission denied? The only permissions you can set for files one the hard disk are "Read only" or "Hidden" from what I can see in properties. Read only is not checked so I can't see anything else I can do to give permission to the file
Posted: Thu Jun 10, 2004 8:23 am
by magicrobotmonkey
stripslashes() - see it looks like its adding a slash to escape the slash in the path.
Posted: Thu Jun 10, 2004 8:53 am
by Chris Corbyn
Just tried stripslashes. It's still giving the same error except with a path that doesn't include double backslashes.
It allows me to open the file in read mode but nothing else.... It must be a filesystem permissions thing in XP.
I don't know of any way to change file permissions on the hard disk other than right click > properties > read only, hidden.
Is there another way to set permissions in windows? WQe're drifting out of PHP here and into Operating Systems....
Posted: Thu Jun 10, 2004 9:12 am
by Chris Corbyn
Not sure if I did this the "I'm not a novice" way or just fudged it to make it work.
I enabled network sharing on the folder and allowed users to change files in that folder. Viola it worked. Technically the network isn't being used but I think it just set some user/group permissions for me (which there must be another way to do than going through the network sharing wizard????).
Thanks for trying to help anyway, much appreciated

Posted: Thu Jun 10, 2004 10:11 am
by feyd
yeah there are disc quota and sharing permission throughout any folder/file, similar to *nix setups.