tags:

views:

437

answers:

4

Hello all,

Is it possible to find PHP tmp folder at run time?

I am uploading files and I am making use of:

$_FILES['upfile']['tmp_name']

However, one of the APIs I am making use of requires that I have to give the full file path of the uploaded file relative to my script! A bit annoying but it has to be done.

Any ideas on how I can do this?

EDIT

Currently. I am doing this, as I know how many levels to go up but I am sure the tmp folder can be at different locations for different servers.

$file = '../..'.$_FILES['upfile']['tmp_name'];

Thanks all

+1  A: 

Really, if possible, I'd recommend you move the uploaded file into its permanent location before you start interacting with other APIs regarding it. You're going to have to anyway, and then you know its relative location easily.

If you can't do that, I suppose you can work out the relative path by analyzing dirname($_FILES['upfile']['tmp_name']) and dirname(__FILE__).

chaos
The API recommends I upload the file fully to my server before passing it on. However, I need this to be faster and I am hoping I can shave off a few seconds!
Abs
You should listen to the API. :) Moving the file to its final location should not take anything nearly as long as 'a few seconds'. The only thing that'd make it take a noticeable amount of time is if PHP's tmp directory and your final storage location are on different file systems, in which case you should certainly make them be on the same file system.
chaos
A: 

getcwd() should output the current working directory.

montooner
A: 
<?php

            // Create a temporary file in the temporary 
            // files directory using sys_get_temp_dir()

    $temp_file = tempnam(sys_get_temp_dir(), 'filename');

    echo $temp_file;



    // on my machine this echoes something like "/tmp/filename7gkPjR"
?>
David Thomas
Is that relative to the current location of the running PHP script?
Abs
Seems to be the absolute path, "cd /tmp/" takes me to the right directory at least.
David Thomas
Ah, sorry, I didn't notice the 'relative to my script' part. I'll see if I can work out a more useful suggestion =)
David Thomas
+1  A: 

Not sure if this is a foolproof way but it works on my server. In addition, there is probably a way to replace folder names with .. but that is beyond me.

$folders = explode('/', $_SERVER['SCRIPT_FILENAME']);
for ($i = 2; $i < count($folders); $i++) $dir .= '../';
$dir = substr($dir,0,-1);
$dir = $dir.$_FILES['upfile']['tmp_name'];
GiladG