php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22177 Warning: fopen() expects parameter 1 to be string
Submitted: 2003-02-11 15:39 UTC Modified: 2003-02-14 11:29 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: laudanp at yahoo dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.3.0 OS: RedHat
Private report: No CVE-ID: None
 [2003-02-11 15:39 UTC] laudanp at yahoo dot com
A php script I have worked fine under PHP 4.2.3, but after the upgrade to PHP 4.3 whenever I try running it I get these errors:

Warning: fopen() expects parameter 1 to be string, resource in ... on line 353

Warning: fwrite(): supplied argument is not a valid stream resource in ... on line 354

Warning: fclose(): supplied argument is not a valid stream resource in ... on line 355

Warning: getimagesize(Resource id #4) [function.getimagesize]: failed to create stream: No such file or directory in ... on line 361

The following blocks of code surround the problematic lines from above:

        if ($input = fopen($image, "rb")) {
                $image_data = fread ($input, 2048576);
                fclose($input);
                if (!$tmp_image = @tmpfile()) {
                        exit("Cannot create tempfile using tmpfile().");
                }
353:                $output = fopen($tmp_image, "wb");
354:                fwrite($output, $image_data);
355:                fclose($output);
356:        } else {
357:                exit("Cannot access ".$image." for reading. (Does not exist or has wrong permissions)");
358:        }
359:
360:        // Create Image from file by type, get and set size
361:        list($width,$height,$type) = getimagesize($tmp_image);
362:

I'm having difficulty determining what the problem is since the code worked with PHP 4.2.3.

Thanks for your help.

Patches

File (last revision 2010-07-09 12:56 UTC by milicicivan3510 at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-11 15:48 UTC] sniper@php.net
RTFM: http://www.php.net/manual/en/function.tmpfile.php

tmpfile() returns a file handle already..NOT filename.
I doubt this ever worked for you as this was not changed in 4.3.0...(you propably used tempnam() instead before)


 [2003-02-11 15:52 UTC] laudanp at yahoo dot com
Per me previous comment addition, the code was not changed whatsoever.  I'm "re-opening" this bug report in the hopes it will gather a response.
 [2003-02-11 15:59 UTC] nicos@php.net
As Jani said, tmpfile returns a resource, no need to try to fopen it. He said also that nothing was changed in 4.3.0 about it, which is right. Your code is bogus and I don't think it ever worked.

See: http://www.php.net/manual/en/function.tmpfile.php

resource tmpfile ( void)

Creates a temporary file with an unique name in write mode, returning a file handle similar to the one returned by fopen(). The file is automatically removed when closed (using fclose()), or when the script ends. 

You should use:
$temp = tmpfile();
fwrite($temp, "writing to tempfile");
fclose($temp); // this removes the file

Not a bug->Bogus again.
 [2003-02-11 16:05 UTC] laudanp at yahoo dot com
I don't consider this bogus and your thoughts are your own.  I wouldn't be the type to just submit a bug report that I didn't think was founded.  The code worked under 4.2.3 and if you care to check I can send the whole package to you to disprove your "thoughts".

Upgrading to php 4.3 has broken the subroutine.  Instead of "thinking" that it doesn't work I would suspect more from php developers for a more quantitative analysis of the situation.

Ergo I prefer to close this bug report myself because I'm apparently not getting any constructive feedback here, instead of having it labeled as "bogus".

Once again, the code, unchanged prior 4.3 worked.  The same exact code that worked no longer does.  Absolutely nothing has been changed.

Anyhow, thanks for replying at least.  It is much better to know where one stands rather than not getting any reply whatsoever.
 [2003-02-11 18:01 UTC] wez@php.net
User error.
Please leave this as bogus.
 [2003-02-11 18:15 UTC] wez@php.net
"Warning: fopen() expects parameter 1 to be string"
Now read the manual for fopen -> parameter 1 needs to be a string.
You are passing a resource.

The fact that this "worked" for you in 4.2.3 indicates a bug in 4.2.3 that has been fixed in 4.3.
Your supposed temporary file name would be a string "Resource id #4", which isn't a very good temporary file name.

You describe the expected behaviour of PHP, your script is broken, therefore this is a user error, and thus a bogus report.

Now, please leave us in peace to fix real bugs.
 [2003-02-11 18:19 UTC] laudanp at yahoo dot com
Quote: "The fact that this "worked" for you in 4.2.3 indicates a bug in 4.2.3 that has been fixed in 4.3."

Now that is an idea that didn't occur to me.  And since php 4.2.3 may have been buggy then obviously the fix is to upgrade to 4.3 and rewrite it the way you folks suggest.

Thanks for that answer, a much more mature one than RTFM.

Hence this isn't bogus but an actual bug that was in 4.2.3.  And the patch is to make sure the code is written correctly.

Thus one last question, looking at the code I initially posted, what would be your suggestion if I may ask specifically tailored to my problem?
 [2003-02-11 18:22 UTC] wez@php.net
For support:
http://www.php.net/support.php
 [2003-02-11 18:22 UTC] laudanp at yahoo dot com
Thanks.
 [2003-02-14 05:29 UTC] hholzgra@php.net
my 2c on this:

yes it worked in 4.2.3 ...

but even in 4.2.3 it didn't do what you wanted,
instead of creating a temp file with a unique
name (or better in addition to it) your fopen
statement opens a file by the name of the 
auto-converted resource number, so if you investigate
this you'll find out that your temporary files
are actualy namend by small numbers, and if
your code always opens the same resources
you'll even get temp files of *equal* names
by different script runs -> not what you want

so 4.3 now warns you about this were 4.3.2 
silently accepted it

"that same exact code block worked 100% under php 4.2.3."
is just not true, as it would create race conditions
if run simultaneous multiple times even in 4.2.3


"Thus one last question, looking at the code I initially posted, what
would be your suggestion if I may ask specifically tailored to my
problem?"

you've been told before in this thread: either use temnam() instead of tmpfile(), or use the file handle resource returned by temfile() right away instead of trying to perform unspecified operations on it ...


"I don't appreciate the RTFM remark, but I am glad you replied back.  I
use the php manual extensively but have not seen that particular link."

so you use functions without reading their specs and then complain about them not working as you'd expect? strange concept ...


 [2003-02-14 11:29 UTC] laudanp at yahoo dot com
Thanks for the reply.  I ran a search on the functions you listed and couldn't find them: 

a) temfile

http://www.php.net/manual-lookup.php?pattern=temfile&lang=en

b) temname

http://www.php.net/manual-lookup.php?pattern=temname&lang=en

Why are they not listed in the manual?

"so you use functions without reading their specs and then complain
about them not working as you'd expect? strange concept ..."

I don't believe in this bug report I ever said the script was written by me.  You see the script that I "have" was released GNU GPL to the public and I was conducting tests on it in two difference versions of PHP.  Because it doesn't function under 4.3 I was trying to be kind in debugging it and releases the "bug fix" to the community-at-large.

This is precisely why I am not 100% familiar with the code because I didn't write it.

This has all been about the gesture of goodwill.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 06:01:29 2024 UTC