PHP way to find the web server's temp path? - php

When you handle an HTTP upload the file is uploaded to
$_FILES['field_name']['tmp_name']
I know I could extract the temp path from there. But I was expecting maybe a $_SERVER param that had the temp path (there's none) or other elegant way of knowing it.
Is there any?

ini_get('upload_tmp_dir');

The function sys_get_temp_dir() returns the directory path used by PHP to store temporary files.

Using XAMPP on my private computer, the ini_get method worked great, since php.ini specifies the value upload_tmp_dir. However, after uploading to my employer's server, this code didn't work because the value apparently didn't exist in his server's php.ini file. So, I used the system's temp dir as the default, and now the script works on my computer and on his server:
$ini_val = ini_get('upload_tmp_dir');
$upload_tmp_dir = $ini_val ? $ini_val : sys_get_temp_dir();

I personally would use Sbm007's suggestion of:
ini_get('upload_tmp_dir');
As this takes into account apache per virtualhost settings like the upload_tmp_dir and open_basedir restrictions, where as :
sys_get_temp_dir()
Would only return the OS's specific temp directory, and if inside a multi-hosted environment could give you a directory you can't write to.

Related

PHP is_writable error returns not writable

I have this code
if(is_writable($destination)){
print "destination is writable";
}
else {
print "destination is not writable";
}
$destination is a relative path like ../subdir/dir/currectDir/ and I have checked and the permission is 777 but this code above returns "destination is not writable" and I don't understand why. Can someone help me? Thanks
There are many reasons why a directory may not be writable, aside from permissions. Some of them are:
The directory is outside of the paths which PHP is able to write to because of open_basedir restrictions
The directory is on a drive which is mounted as read-only
The directory has immutable or appendonly attributes set
The filesystem is corrupt
However, my guess in this case would be that the path you are using is not correct. Try running
echo getcwd();
Just before your is_writable() call.
It will tell you the current directory the PHP script is running in, and from there you should be able to work out the correct relative path. Not that if running a PHP script from the command line its path will be that of the current directory, whereas running via a web server such as Apache it will usually be the public_html or httpdocs directory. It may have also been changed earlier on in the script execution.
You can also specify an absolute path (e.g. /home/youruser/public_html/subdir/dir/currectDir)
I can't comment, so I have to respond here but it looks as though the path (you posted a relative path, not an absolute path by the way) is incorrect - have you tried
var_dump(file_exists($destination));
first, to determine if you are looking in the right place for your directory?

PHP - uploading to dir outside docroot

I have nginx+php-fastcgi running on my server and I"m trying to allow a php script in /var/www/contest to upload to a non-public directory located at /var/www/private/uploads.
I've tried changing the include path in php.ini but all I get is "No input file specified." when I try to view the page in /contest that uploads the pics.
your php script will run as some user. give that user write access to /var/www/private/uploads.
At first, check your upload_tmp_dir in PHP_INI system and set to right path.
Check write permissions for it.
Use move_uploaded_file to store files manually in right place.

How to change the temp upload path runtime?

I would like to change my applications temp path to a subfolder, so that users on a shared server cannot see any uploaded files.
I would like to be able to do this run-time, or via .htaccess if possible (although I would like the new temp path to be a subdir of the original temp path). I can't edit the php.ini on the shared server.
I know I can check what the tmp path is via sys_get_temp_dir(), but there doesn't seem to be a way to set it.
Is this even possible?
ini_set('upload_tmp_dir','your/path/here/');
The temporary directory used for
storing files when doing file upload.
Must be writable by whatever user PHP
is running as. If not specified PHP
will use the system's default.
If the directory specified here is not
writable, PHP falls back to the system
default temporary directory. If
open_basedir is on, then the system
default directory must be allowed for
an upload to succeed.
upload_tmp_dir
maybe in '11, now not anymore.
As documented here ini.list
and with reference to modes,
'upload_tmp_dir' cannot be changed at run time.
thanks.

PHP: is_readable not working for relative path, works for absolute

I'm moving a site from one server to another. On the old server, my code calls is_readable("filename") and it works. On the new server, it does not work. The files are exactly the same and "filename" is in the same place relative to the calling page.
When I put in an absolute path instead, is_readable returns true as expected. Any suggestions about what the problem could be?
safe_mode is off and open_basedir is not set in my php.ini. I also modified the file permissions, it doesn't work even if I chmod 777 (but that shouldn't matter since it reads properly when using the absolute path).
The servers probably have different configurations causing the current working directory (CWD) not to be the one where the script is being read. Relative paths are always relative to the CWD, not the current executing script.
You can check the CWD by calling getcwd() or by using realpath() to resolve the relative path into an absolute one. If the value is incorrect, you will have to either configure the server properly, or set the CWD manually by doing the following:
chdir(dirname(__FILE__));
Try using realpath on you relative path to check if it points to the right file.
You could also use getcwd to check if you're in the correct directory.
Typically, if you pass a relative path to is_readable (or file_exists), it will return false unless the path happens to be relative to the current PHP direct -- View PHP chdir info

Deleteing files in the root dir of my dedicated server with PHP

I have a apache dedicated server with lost of websites.
I also have a red5 installation on the server.
What I want to know how to do is perform file functions - specifically unlink() - on files held in the RED5 directory within the root server dir.
I can move files with this:
copy ("http://www.parttimepornstar.com:5080/echo/streams/".$strFilename, $strDestination);
but
unlink("http://www.parttimepornstar.com:5080/echo/streams/".$strFilename);
...won't work...
Any ideas what I'm doing wrong?
Thanks.
You need to check the permission of the file that you are trying to delete. Apache should (hopefully) not be running as root and therefore cannot delete any files that it does not have permissions for.
You should also be vary wary of security. Allowing an unchecked variable to be used in the end of a copy() or unlink() call could potentially give a user access to your entire filesystem. Take a look at basename.
I suspect you need to use absolute file paths rather than urls/relative paths. Also if you want to delete from root dir, you need to specify that too. Try doing something like below:
unlink($_SERVER['DICUMENT_ROOT'] . '/RED5/' . $yourfiles);
You are addressing the files via HTTP, which you can't use to delete files.
You need to specify a filesystem path such as /etc/httpd/sitename/file.php

Categories