Temporary folder in php not accessible


This was an unforgettable experience that i knew when i was developing a website that's hosted on a web server. If you're planning to use php mail function with attachment then you'll certainly upload those attachments temporarily to the web server before sending it to the recipient.

These attachments will be stored into the folder specified in 'upload_tmp_dir' directive in php.ini. So, unless you specified another value for the temporary folder, it always point to '/tmp/php_upload' by default.

Example:
When i wrote a code like this (for mail attachment):  

  1. $_FILES["yourfile"]["tmp_name"]

The error message comes up : fopen() [function.fopen]: open_basedir restriction in effect. File(/tmp/php_upload/phpk3qSgH) is not within the allowed path(s).

Note: Modifying .htaccess to setup and redirect from '/tmp/php_upload' to your own temporary directory such as 'public/www/temp' folder and gave it chmod 777 to be accessible by anyone won't work. Files by default are stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini

Two simple tasks to perform:
1. Setup your own temporary directory, for ex. /public/www/temp folder
2. Email your web server admin to change the directive of upload_tmp_dir in php.ini to your own temp directory, as you don't have access to it.

Woila, now you can upload your files temporarily to web server and after it finished, the contents in temp folder will automatically be disposed.