PHP Exercises : Get the directory path used for temporary file
PHP : Exercise-28 with Solution
Write a PHP script to get the directory path used for temporary files.
Sample Solution:
PHP Code:
<?php
// Generate a temporary file with a unique filename
// The tempnam() function creates a temporary file with a unique name in the system's temporary directory
$temp_file = tempnam(sys_get_temp_dir(), 'Tux');
// Output the path of the created temporary file
echo $temp_file."\n";
?>
Explanation:
- Generate a Temporary File:
- The tempnam() function is used to create a temporary file. It takes two arguments:
- sys_get_temp_dir(): This function returns the path of the system's temporary directory.
- 'Tux': This string is used as a prefix for the unique filename.
- Store Temporary File Path:
- The path of the created temporary file is stored in the variable $temp_file.
- Output Temporary File Path:
- The echo statement outputs the path of the created temporary file, followed by a newline (\n).
Output:
/tmp/TuxN0kaAE
Flowchart:
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script to print out all the credits for PHP.
Next: Write a PHP script to get the names of the functions of a module.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/php-exercises/php-basic-exercise-28.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics