php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48271 fopen creates two files
Submitted: 2009-05-14 02:37 UTC Modified: 2009-05-15 00:29 UTC
From: my dot email dot subscriptions at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.2.9 OS: windows xp spIII, Apache 2.2.3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: my dot email dot subscriptions at gmail dot com
New email:
PHP Version: OS:

 

 [2009-05-14 02:37 UTC] my dot email dot subscriptions at gmail dot com
Description:
------------
Using this code I get two files written in my user directory.
$thisFilePointer = fopen( $tempFileName, 'x' );
die;
The first one is the file I named as "thisFile1.png", the second is the same name plus 1, that is 'someFile2.png', both files are empty.

If I write an image, the first and second files contains two different images, one is the image I want the other is alike but not the same content.

If the file is a pdf file, I get two files also but the same content in both of them.



Reproduce code:
---------------
$thisFile        = $pdfFileName . $fileType;
$tempFileName    = tempFilesDir . $thisFile;
$thisFilePointer = fopen( $tempFileName, 'x' );
if  ( !$thisFilePointer ) {
    # Here we prepare the error output into 'userErr';
    trigger_error( userErr, E_USER_ERROR );
} //if
$fileHandler = fwrite( $thisFilePointer, $pdfContent );
if  ( !$fileHandler ) {
    # Here we prepare the error output into 'userErr';
    trigger_error( userErr, E_USER_ERROR );
} //if
fclose( $thisFilePointer );

Expected result:
----------------
Get only one file with the desired content.

This did not happened to me under php 4


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-14 08:04 UTC] pajoye@php.net
Your code is not usable, please provide a self contained script, something like:

$fp = fopen('a.txt');
if (!$fp) die("cannot open a.txt\n");
$bytes = fwrite($fp, 'abc');
if (!$bytes)  die("cannot write to a.txt\n");
fclose($fp);
 [2009-05-14 17:10 UTC] my dot email dot subscriptions at gmail dot com
Following your lead:

$fp = fopen('a1.txt', 'x');
if (!$fp) die("cannot open a1.txt\n");
fclose($fp);
Expected result, 1 empty file.
Resulted in two empty files, a1.txt, a2.txt

------------------------------------------
$fp = fopen('a1.gif', 'x');
if (!$fp) die("cannot open a1.gif\n");
fclose($fp);
Expected result, 1 empty file.
Resulted in two empty files, a1.gif, a2.gif

------------------------------------------
$fp = fopen('a1.pdf', 'w');
if (!$fp) die("cannot open a1.pdf\n");
fclose($fp);
Expected result, 1 empty file.
Resulted in two empty files, a1.pdf, a2.pdf

------------------------------------------
$fp = fopen('a1.txt', 'x');
if (!$fp) die("cannot open a1.txt\n");
$bytes = fwrite($fp, 'Some text');
if (!$bytes)  die("cannot write to a1.txt\n");
fclose($fp);
Expected result, 1 txt file.
Resulted in two txt files, a1.txt, a2.txt both files have the same text.

------------------------------------------
$fp = fopen('a1.gif', 'x');
if (!$fp) die("cannot open a1.gif\n");
$bytes = fwrite($fp, gifimage);
if (!$bytes)  die("cannot write to a1.gif\n");
fclose($fp);
Expected result, 1 image file.
Resulted in two different images, a1.gif, a2.gif.
The image in a2.gif is the expected image and a1.gif is another different image.

------------------------------------------
$fp = fopen('a1.pdf', 'w');
if (!$fp) die("cannot open a1.pdf\n");
$bytes = fwrite($fp, pdfcontent);
if (!$bytes)  die("cannot write to a1.pdf\n");
fclose($fp);
Expected result, 1 pdf file.
Resulted in two pdf files, a1.pdf, a2.pdf both files have the same pdf content.

------------------------------------------
------------------------------------------
NOW PLEASE, BEAR WITH ME.

$fp = fopen('a1.gif', 'x');
if (!$fp) die("cannot open a1.gif\n");
Expected result, 1 empty file.
Resulted in two empty files, a1.gif, a2.gif

As I already know that it will write two files, then I do this.

$fp = fopen('a2.gif', 'w');
if (!$fp) die("cannot open a2.gif\n");
$bytes = fwrite($fp, gifimage);
if (!$bytes)  die("cannot write to a2.gif\n");
fclose($fp);
Expected result, 1 image file in a2.gif.
Resulted in two empty files, a1.gif, a3.gif and two different images in files a2.gif and a4.gif.
The image in a4.gif is the expected image and a2.gif is another different image.


I hope this time the code is "usable" and the problem is clear.
 [2009-05-14 17:19 UTC] pajoye@php.net
hi,

I don't think you are really using the right code to test.

there is no way that:

$fp = fopen('a1.txt', 'x');
if (!$fp) die("cannot open a1.txt\n");
fclose($fp);

produce two files, a1.txt and a2.txt. Same for all other examples. Please run it in the CLI if you are not sure what files you are running via apache.
 [2009-05-14 20:12 UTC] my dot email dot subscriptions at gmail dot com
I just tested a special script that resembles the working script in two forms.

When running the script using the browser, it does create two files.

Then when using CLI, it creates only one.

To see the test, you can use this URL:
http://www.web-carlos.com/Testing/TxtIndex.php
This will give you back the name of the created file, lets say: 'TxtIndex11.txt' to see the problem use the browser and ask for both files like this:
http://www.web-carlos.com/WebTempFiles/TxtIndex11.txt
http://www.web-carlos.com/WebTempFiles/TxtIndex12.txt

Both files will have the same content.

To see the whole script used, browse this url:
http://www.web-carlos.com/Testing/TxtIndex.txt

I have also place a phpinfo if you want to see the server configuration, just browse:
http://www.web-carlos.com/Testing/phpinfo.php

Believe it or not, but it does create two files using the browser, of course I can't use CLI on the server but I do, at my development pc.
 [2009-05-14 21:50 UTC] jani@php.net
Please check your access log for this test script being accessed. I bet 
you get 2 requests to it instead of one when you access it with browser.
 [2009-05-14 21:52 UTC] jani@php.net
And accessing your test shows it only creates one file for me. So it's 
propably some braindead browser issue you're having. (I tested with 
Chrome..)
 [2009-05-14 22:39 UTC] my dot email dot subscriptions at gmail dot com
Thanks for your guidance, you have saved me a lot of work.

As you suggested, I tried it with another browser(s) (opera, chrome & IE) and it works as it should so the problem is browser (Firefox) related.

I have one more favor to ask, what can I do, and how, to be able to report this behavior to Firefox after a thorough investigation?.

Thank you very much for your help and guidance.
 [2009-05-15 00:29 UTC] jani@php.net
http://www.ehow.com/how_2033393_report-bugs-firefox.html

And also note: I can not reproduce this with any browser. Firefox 3.0.10  
works just fine too.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC