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)
- 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 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.
- $ 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 );