FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » fopen error
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
fopen error [message #175227] Sun, 28 August 2011 00:56 Go to next message
eBob.com is currently offline  eBob.com
Messages: 11
Registered: August 2011
Karma: 0
Junior Member
This fopen fails and I don't understand why ...

$fpFileOut = fopen($fileout,"c");
if (!$fpFileOut) {echo "Error opening $fileout\n"; echo E-WARNING; exit;}

The file does not exist but shouldn't "c" create it? My understand of "c"
is that it's created if it doesn't exist and overwrites the file if it does
exist. (That's not exactly what the doc says but that is the way I
understand it.)

My attemp to echo E-WARNING produces "0". I have no idea what's up with
that.

Probably a dumb noob error as I am a dumb noob.

Thanks, Bob
Re: fopen error [message #175228 is a reply to message #175227] Sun, 28 August 2011 03:55 Go to previous messageGo to next message
BKDotCom is currently offline  BKDotCom
Messages: 7
Registered: October 2010
Karma: 0
Junior Member
On Aug 27, 7:56 pm, "eBob.com" <eBob....@totallybogus.com> wrote:
> This fopen fails and I don't understand why ...
>
> $fpFileOut = fopen($fileout,"c");
> if (!$fpFileOut) {echo "Error opening $fileout\n";  echo E-WARNING; exit;}
>
> The file does not exist but shouldn't "c" create it?  My understand of "c"
> is that it's created if it doesn't exist and overwrites the file if it does
> exist.  (That's not exactly what the doc says but that is the way I
> understand it.)

Does the directory you're attempting to create the file in exist?
If it does, do you have proper rights to write in it?
Or does the file already exist and you don't have rights to modify it?


> My attemp to echo E-WARNING produces "0".  I have no idea what's up with
> that.

"If the open fails, an error of level E_WARNING is generated. You may
use @ to suppress this warning. "

See http://www.php.net/manual/en/book.errorfunc.php

Nutshell E_WARNING is a constant signifying the severity of the error
not a variable describing the last error

To see the actual error open the error log file.

Or
error_reporting(E_ALL ^ E_NOTICE); // report all errors except for
notices
// just in case it's
currently set to to report no errors
ini_set('display_errors',1); // output the error
// (in addition to writing it
to error log)
Or, if you've got PHP >= 5.2, you can try
print_r(error_get_last());

anyhow, once you view see the error, you should know the reason for
the error. :)
Re: fopen error [message #175230 is a reply to message #175227] Sun, 28 August 2011 15:23 Go to previous messageGo to next message
A is currently offline  A
Messages: 17
Registered: June 2011
Karma: 0
Junior Member
> $fpFileOut = fopen($fileout,"c");
> if (!$fpFileOut) {echo "Error opening $fileout\n"; echo E-WARNING; exit;}

do an echo $fileout; before fopen to see what the folder/file is.

remember the following:

- in linux/unix the filenames are case sensitive and file system is a bit
different than windows
- when you define $fileout remember if you use backslashes you need to
escape them so it is not
$fileout = "c:\folder\file.txt";
rather
$fileout = "c:\\folder\\file.txt";

in php you can use forward slashes instead which don't require escaping:

$fileout = "c:/folder/file.txt";

- you need to have write permissions (755, 666 etc.) if it is unix/linux
system. many hosters set this for PHP but you haven't specified what kind of
host you test this on.

it is probably my lazyness, but unless you really need fopen you can get by
just fine with much easier functions like readfile, file(),
file_get_contents(), file_put_contents()... all work done in a single line
rather than fopen, fread, fwrite, fclose.
Re: fopen error [message #175231 is a reply to message #175227] Sun, 28 August 2011 15:27 Go to previous messageGo to next message
A is currently offline  A
Messages: 17
Registered: June 2011
Karma: 0
Junior Member
also remember that single and double quotes don't behave the same in php -
escapes don't work on single quotes.

<?php
$abc = "\r\naaa\tbbb";
echo $abc;

$abc = '\r\naaa\tbbb';
echo $abc;
?>
Re: fopen error [message #175232 is a reply to message #175228] Sun, 28 August 2011 16:00 Go to previous messageGo to next message
eBob.com is currently offline  eBob.com
Messages: 11
Registered: August 2011
Karma: 0
Junior Member
Thanks very much BK. Those tips are going to make my debugging much easier.
(Why don't books mention such aids at the outset?)

My mistake was a dumb logic/typo error which tired eyes were unable to see
last night, but which fresh eyes spotted this morning.

Thanks again, Bob


"BKDotCom" <kent(dot)brad(at)gmail(dot)com> wrote in message
news:781ed9c7-a89d-48eb-bf2b-3aefad6be6bf(at)b9g2000prd(dot)googlegroups(dot)com...
On Aug 27, 7:56 pm, "eBob.com" <eBob....@totallybogus.com> wrote:
> This fopen fails and I don't understand why ...
>
> $fpFileOut = fopen($fileout,"c");
> if (!$fpFileOut) {echo "Error opening $fileout\n"; echo E-WARNING; exit;}
>
> The file does not exist but shouldn't "c" create it? My understand of "c"
> is that it's created if it doesn't exist and overwrites the file if it
> does
> exist. (That's not exactly what the doc says but that is the way I
> understand it.)

Does the directory you're attempting to create the file in exist?
If it does, do you have proper rights to write in it?
Or does the file already exist and you don't have rights to modify it?


> My attemp to echo E-WARNING produces "0". I have no idea what's up with
> that.

"If the open fails, an error of level E_WARNING is generated. You may
use @ to suppress this warning. "

See http://www.php.net/manual/en/book.errorfunc.php

Nutshell E_WARNING is a constant signifying the severity of the error
not a variable describing the last error

To see the actual error open the error log file.

Or
error_reporting(E_ALL ^ E_NOTICE); // report all errors except for
notices
// just in case it's
currently set to to report no errors
ini_set('display_errors',1); // output the error
// (in addition to writing it
to error log)
Or, if you've got PHP >= 5.2, you can try
print_r(error_get_last());

anyhow, once you view see the error, you should know the reason for
the error. :)
Re: fopen error [message #175233 is a reply to message #175230] Sun, 28 August 2011 16:07 Go to previous messageGo to next message
eBob.com is currently offline  eBob.com
Messages: 11
Registered: August 2011
Karma: 0
Junior Member
Thanks A. I was able to find my dumb typo/logic error this morning. Thanks
for making me aware of the file_get_contents and file_put_contents
functions. I like to make my code as concise as possible.

Incidentally ... I am using single backslashes on the (Windows) command line
to specify file names and somehow that seems to be working fine. I'm using
PHP 5.3.

Thanks again,

Bob

"A" <a(at)a(dot)a> wrote in message news:j3dml8$3kh$1(at)gregory(dot)bnet(dot)hr...
>> $fpFileOut = fopen($fileout,"c");
>> if (!$fpFileOut) {echo "Error opening $fileout\n"; echo E-WARNING;
>> exit;}
>
> do an echo $fileout; before fopen to see what the folder/file is.
>
> remember the following:
>
> - in linux/unix the filenames are case sensitive and file system is a bit
> different than windows
> - when you define $fileout remember if you use backslashes you need to
> escape them so it is not
> $fileout = "c:\folder\file.txt";
> rather
> $fileout = "c:\\folder\\file.txt";
>
> in php you can use forward slashes instead which don't require escaping:
>
> $fileout = "c:/folder/file.txt";
>
> - you need to have write permissions (755, 666 etc.) if it is unix/linux
> system. many hosters set this for PHP but you haven't specified what kind
> of host you test this on.
>
> it is probably my lazyness, but unless you really need fopen you can get
> by just fine with much easier functions like readfile, file(),
> file_get_contents(), file_put_contents()... all work done in a single line
> rather than fopen, fread, fwrite, fclose.
>
Re: fopen error [message #175234 is a reply to message #175227] Sun, 28 August 2011 16:36 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sat, 27 Aug 2011 20:56:44 -0400, eBob.com wrote:

> This fopen fails and I don't understand why ...
>
> $fpFileOut = fopen($fileout,"c");
> if (!$fpFileOut) {echo "Error opening $fileout\n"; echo E-WARNING;
> exit;}

If you're doing this at the command line, you should get an error message
of some sort.

If you're doing it on a web server, it should write an error message
somewhere.

The error message may have more information.

Look on the php documentation website for error handling functions to
obtain the error message if you can't find it anywhere else.

Rgds

Denis McMahon
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: mktime 2-digit vs 4-digit year
Next Topic: OpenSSL, openssl_pkey_get_private
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 01:35:53 GMT 2024

Total time taken to generate the page: 0.02393 seconds