Bug #64811 [Com]: SplTempFileObject doesn't provide valid return values for SplFileInfo / filenam
Edit report at https://bugs.php.net/bug.php?id=64811&edit=1
ID: 64811
Comment by: hudsonHatesSpam at gmail dot com
Reported by: bugs+php at childno dot de
Summary: SplTempFileObject doesn't provide valid return
values for SplFileInfo / filenam
Status: Open
Type: Bug
Package: SPL related
Operating System: Any
PHP Version: 5.4.15
Block user comment: N
Private report: N
New Comment:
In my use case, I'm trying to build a temp file for a MySQL "LOAD DATA LOCAL INFILE".
I do not want the file to persist after the script has run, and there's no clear way to remove
an SplFileObject at the end of the script. My recourse was to use SplTempFileObject calling:
$tempFile = new \SplTempFileObject(0);
According to the documentation:
"public SplTempFileObject::__construct ([ int $max_memory ] )"
and
"If max_memory is zero, no memory will be used"
which forces the tempFile to be written to disk. While I can see my temp file during script
execution, I have no way of feeding the file URI to my SQL call.
Previous Comments:
------------------------------------------------------------------------
[2013-05-10 11:20:50] bugs+php at childno dot de
Description:
------------
the SplTempFileObject is intended to use the php://temp in-memory storage.
This has disadvantages if it is used for creating files on the fly that needed to be processed
afterwards. Typically you want to provide SplFileInfo to certain interfaces that might handle and
read the content again.
e.g. see
http://stackoverflow.com/questions/12555139
Test script:
---------------
$file = new \SplTempFileObject();
if ($file->fwrite("foo") !== null)
print('wrote ' . $file->ftell() . ' byte(s) to ' .
$file->getRealPath() . '');
// wrote 3 byte(s) to ``
// ^^ thats ok for < 2MB while it is expected to be in-memory
$file = new \SplTempFileObject(0);
if ($file->fwrite("foo") !== null)
print('wrote ' . $file->ftell() . ' byte(s) to ' .
$file->getRealPath() . '');
// wrote 3 byte(s) to ``
// NOT OK Because we set the limit to 0 to force writing stuff to the filesystem
$file = new \SplFileObject(tempnam(sys_get_temp_dir(), rand()), 'w+');
if ($file->fwrite("bar") !== null)
print('wrote ' . $file->ftell() . ' byte(s) to ' .
$file->getRealPath() . '');
// wrote 3 byte(s) to /tmp/2123740490ZwI6Qd
// ^^ that's what I expect even for \SplTempFileObject(0)
Expected result:
----------------
\SplTempFileObject->getFileName() should return s.th. like 2123740490ZwI6Qd
\SplTempFileObject->getFileInfo() should return a valid SplFileInfo Object pointing to
/tmp/2123740490ZwI6Qd
\SplTempFileObject->getRealPath() should return s.th. like /tmp/2123740490ZwI6Qd
Actual result:
--------------
\SplTempFileObject->getFileName() will return php://temp \SplTempFileObject->getFileInfo()
will return an empty object / file pointer \SplTempFileObject->getRealPath() will always return
an empty string!
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64811&edit=1
Thread (2 messages)