Difference between tempnam and tmpfile created by php

Source: Internet
Author: User
Tags php tutorial

Syntax
Tempnam (dir, prefix)


*/
Function dir_wriable ($ dir) // customize function extension to create a temporary file
{
$ Test = tempnam ("$ dir", "test_file"); // create a temporary file
If ($ fp = @ fopen ($ test, "w") // if the file is successfully opened
  {
@ Fclose ($ fp); // close the file
@ Unlink ($ test); // delete an object
$ Wriable = "ture"; // The return value is true.
  }
Else
  {
$ Wriable = false or die ("cannot open $ test! "); // The returned value is false.
  }
Return $ wriable; // returns a boolean value.
}
If (dir_wriable (str_replace ('/', '/', dirname (_ file _) // call a UDF
{
$ Dir_wriable = 'File created successfully ';
}
Else
{
$ Dir_wriable = 'File creation failed ';
}

/*
If the php Tutorial cannot create a file in the specified dir parameter, it is returned to the default value of the system.

Note: the behavior of this function has changed in version 4.0.3. A temporary file will also be created to avoid competition, that is, there may be a file with the same name in the file system between the string generated as the file name and the script to actually create the file. Note: If the file is no longer needed, the file will not be deleted automatically.

The tmpfile () function creates a temporary file with a unique file name in read/write (w +) mode.

The file will be automatically deleted after it is closed (with fclose (), or after the script is completed.


*/

$ Temp = tmpfile ();

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

// Returns the start of the file.
Rewind ($ temp );

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

// Delete an object
Fclose ($ temp );

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.