Difference between tempnam and tmpfile created by php

Source: Internet
Author: User
The tempnam () function creates a temporary file with a unique file name. if the file name is successfully created, the function returns a new temporary file name. if the file name fails, false is returned.

The tempnam () function creates a temporary file with a unique file name. if the file name is successfully created, the function returns a new temporary file name. if the file name fails, false is returned.

Syntax: tempnam (dir, prefix)

  1. Function dir_wriable ($ dir) // customize function extension to create a temporary file
  2. {
  3. $ Test = tempnam ("$ dir", "test_file"); // create a temporary file
  4. If ($ fp = @ fopen ($ test, "w") // if the file is successfully opened
  5. {
  6. @ Fclose ($ fp); // close the file
  7. @ Unlink ($ test); // delete an object
  8. $ Wriable = "ture"; // The return value is true.
  9. }
  10. Else
  11. {
  12. $ Wriable = false or die ("cannot open $ test! "); // The returned value is false.
  13. }
  14. Return $ wriable; // returns a boolean value.
  15. }
  16. If (dir_wriable (str_replace ('/', '/', dirname (_ file _) // call a UDF
  17. {
  18. $ Dir_wriable = 'File created successfully ';
  19. }
  20. Else
  21. {
  22. $ Dir_wriable = 'File creation failed ';
  23. }

If php 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. after the file is closed, it is automatically deleted using fclose (), or after the script is completed.

  1. $ Temp = tmpfile ();
  2. Fwrite ($ temp, "testing, testing .");
  3. // Returns the start of the file.
  4. Rewind ($ temp );
  5. // Read 1 k from the file
  6. Echo fread ($ temp, 1024 );
  7. // Delete an object
  8. 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.