PHP Create temporary files Tempnam and tmpfile differences _php Tutorial

Source: Internet
Author: User
The Tempnam () function creates a temporary file with a unique file name. If successful, the function returns a new temporary file name. If it fails, it returns false.

The Tempnam () function creates a temporary file with a unique file name.

If successful, the function returns a new temporary file name. If it fails, it returns false.

Grammar
Tempnam (Dir,prefix)


*/
function dir_wriable ($dir)//Custom Function extension Create temporary file
{
$test =tempnam ("$dir", "test_file"); Create a temporary file
if (fopen ($test, "w" > $fp = @fopen ($test, "w"))//If the file opened successfully
{
@fclose ($FP); Close File
@unlink ($test); deleting files
$wriable = "Ture"; return value is True
}
Else
{
$wriable =false or Die ("Cannot open $test!"); The return value is False
}
return $wriable; Returns a Boolean value
}
if (Dir_wriable (Str_replace ('//', '/', dirname (__file__))))//Call Custom function
{
$dir _wriable= ' success in document Creation ';
}
Else
{
$dir _wriable= ' failed to build the file ';
}

/*
If the PHP tutorial is unable to create the file in the specified dir parameter, it is returned to the system default value.

Note: The behavior of this function has changed in version 4.0.3. A temporary file is also created to avoid a competitive situation, which is the possibility that a file with the same name will exist in the file system between a string that is generated as a file name and a script that actually establishes the file. Note that if you no longer need the file, you will delete the file and not automatically delete it.

The Tmpfile () function establishes a temporary file with a unique file name in read-write (w+) mode.

The file is automatically deleted after it is closed (with fclose ()) or when the script is finished.


*/

$temp = Tmpfile ();

Fwrite ($temp, "testing, testing.");

Rewind the beginning of the file
Rewind ($temp);

Read 1k from a file
Echo fread ($temp, 1024);

deleting files
Fclose ($temp);

http://www.bkjia.com/PHPjc/632346.html www.bkjia.com true http://www.bkjia.com/PHPjc/632346.html techarticle the Tempnam () function creates a temporary file with a unique file name. If successful, the function returns a new temporary file name. If it fails, it returns false. The Tempnam () function creates a ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.