Jump to content

php fopen creates multiple files at once


MyWebAlias

Recommended Posts

Hi, everybody.

 

I have this strange behavior in php.

 

With the little script shown below, I get two files created on the same directory, the first one is the file I named in "thisFile", the second is the same name plus 1, that is 'someFile2.txt'.

 

Has anyone had this kind of problem?

 

    $thisFile        = 'someFile1.txt;

    $tempFileName    = 'UserDir' . $thisFile;

    $thisFilePointer = fopen( $tempFileName, 'x' );

 

I'm running Apache 2,2.11 and php 5.2.9-2

This has never happened before.

 

Any clue as to how to solve this issue will be greatly appreciated.

 

Carlos.

Thanks for asking, w3evolutions.

 

Here is some more details, hope you get my problem.

 

The idea of this work is to send the user a requested pdf with the required info.

 

First we prepare the pdf output (long code).

Then comes the sending of the pdf file to the user, and used code is below.

 

The specific problem I'm facing is this.

On line 20, the file is created but instead of creating one creates two files.

One file with the given and desire name ( tempFileName ) and another with the very same name plus one for number.

I found this adding a (die) after line 20 and looking into the temp directory and there, there were two files with no content of course.

 

What is worst is that in line 25 where the file is written it writes the very same data in both files

 

What is worst of worst is that if I create an image for a captcha, it writes two images with two different contents (don't know how), how about that?, so it messes up the script because the 2nd image is the one I need but the rutine (another basically the same) gives me back the associated FIRST file name.

 

0000 --------------------------------------------------------

0001 <?php

0002 #  Create the pdf content.

0003 ---------long code--------

0004 $pdfContent  = $pdf->ezOutput( );

0005

0006 #  Here we proceed to write the pdf content to disc.

0007 $pdfFileName = WriteTheFiles( $pdfContent, $fileType );

0008

0009 #  Here we proceed to output the pdf file to the user.

0010 OutputThePdf( $pdfFileName );

0011

0012 #  Here we proceed to output the pdf file to the user.

0013 function WriteTheFiles( $pdfContent, $fileType ) {

0014    # There is some code to get the last consecutive number

0015    # of the same type of file.

0016    # This number is used to create $pdfFileName like this:

0017    # $pdfFileName = 'PdfOutpt' . number + 1.

0018    $thisFile        = $pdfFileName . $fileType;

0019    $tempFileName    = tempFilesDir . $thisFile;

0020    $thisFilePointer = fopen( $tempFileName, 'x' );

0021    if  ( !$thisFilePointer ) {

0022        # Here we prepare the error output into 'userErr';

0023        trigger_error( userErr, E_USER_ERROR );

0024    } //if

0025    $fileHandler = fwrite( $thisFilePointer, $pdfContent );

0026    if  ( !$fileHandler ) {

0027        # Here we prepare the error output into 'userErr';

0028        trigger_error( userErr, E_USER_ERROR );

0029    } //if

0030    fclose( $thisFilePointer );

0031    return( $tempFileName );

0032 }

0033

0034 #  Proceed to open the PDF file in a new window.

0035 function OutputThePdf( $pdfFileName ) {

0036    echo "

0037      <html>

0038      <head>

0039        <script language='JavaScript'>

0040        function go_now( ) {

0041            location.href = '" . $pdfFileName . "';

0042        }

0043        </script>

0044      </head>

0045      <body onLoad='go_now( )'; >

0046      </body>

0047      </html>

0048 ";

0049 }

0050 ?>

0051 --------------------------------------------------------

 

Thanks for the code advice but I wanted to show the numbering of the lines.

 

As of trying fopen( $tempFileName, 'w+' ) I tried with the same results, that was my initial statement then I changed it to 'x' to ensure the file did not exist.

 

When you issue ezOutput() then the variable, in this case $pdfContent, will have the full content of the pdf you want to output.

 

Also, just like you, i have no clue as to how and why is creating two files.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.