note 120345 added to function.tmpfile

From: Date: Fri, 16 Dec 2016 20:18:56 +0000
Subject: note 120345 added to function.tmpfile
Groups: php.notes 
Request: Send a blank email to php-notes+get-208609@lists.php.net to get a copy of this message
It's not obvious, but if you create method within any class and use tempfile() in within, then returning path to this temp file - you wont get the file, it's already removed on the return from function. To avoid that, you need to keep reference outside of the used function and just pass it as variable. <?php class A { public function writeToFile( $tempFile) { fwrite($tempFile, "something"); } public function doSomething() { $tempFile = tmpfile(); $this->writeToFile($tempFile); // file is still living here, because variable with reference is in sacope } } class B { public function writeToFile() { $tempFile = tmpfile(); fwrite($tempFile, "something"); $tempPath = stream_get_meta_data($tempFile); return $tempPath['uri']; } public function doSomething() { $tempFile = tmpfile(); $path = $this->writeToFile($tempFile); // file is already gone, $path is pointing to non-existent } } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 178.41.202.149 ---- Manual Page -- http://php.net/manual/en/function.tmpfile.php Edit -- https://master.php.net/note/edit/120345 Del: integrated -- https://master.php.net/note/delete/120345/integrated Del: useless -- https://master.php.net/note/delete/120345/useless Del: bad code -- https://master.php.net/note/delete/120345/bad+code Del: spam -- https://master.php.net/note/delete/120345/spam Del: non-english -- https://master.php.net/note/delete/120345/non-english Del: in docs -- https://master.php.net/note/delete/120345/in+docs Del: other reasons-- https://master.php.net/note/delete/120345 Reject -- https://master.php.net/note/reject/120345 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#208609) next »