...

View Full Version : relative path from upload_tmp_dir root folder?



singedpiper
01-06-2006, 04:06 PM
ini_get('upload_tmp_dir') returns "PHP\uploadtemp" on my server. This is a relative path from some known folder.

move_uploaded_file takes two parameters that are relative paths from this known folder.

the first can be gotten easily... it's $_FILES["file"]["tmp_name"]

the second needs to be constructed like

$relativePath."files/".$_FILES['file']['name']

where $relativePath is "www/localhost/music/" for my server...

this is the relative path from the known folder to my current directory.


My Question is as Follows:

how can I obtain $relativePath dynamically so as to allow my code to run on different servers?

angst
01-06-2006, 04:21 PM
try this:
$_SERVER['DOCUMENT_ROOT']

-SiX

singedpiper
01-06-2006, 04:56 PM
that's outputting : C:/Documents and Settings/Charlie/Desktop/no hassle php/PortableWebAp3.1/program/www/localhost

whereas, I only want the relative path to document root: www/localhost

thanks for the attempt though

angst
01-06-2006, 04:59 PM
well,
for only a relative path, can you just use this? "music/"

singedpiper
01-06-2006, 05:33 PM
Ithought I could, but it seems that for the purposes of moving temporary upload files to an actual location, you need to use a relative path from the folder one level above www/

unfortunately, I want to be able to package my code for use on other servers... who might run it in various subdirectories... and I'm not certaing that all servers work the same way.

the worst part is that it doesn't seem to allow the use of absolute paths.

angst
01-06-2006, 06:02 PM
hmm,
ok,, best I could come up with is this:




<?php
Function GetRelativePath($page){
$path = $_SERVER['REQUEST_URI'];
$result = str_replace($page,"",$path);
return $result;
}

//give the function the current page name
echo GetRelativePath("path.php");
?>



hope this helps,

-SiX

firepages
01-06-2006, 07:11 PM
The file upload example at PHP.net uses an absolute path, on windows 98 the spaces in the path might be an issue though not on XP etc, can you post upload/move code ?

angst
01-06-2006, 07:12 PM
just wondering,
did this function work?
I never had a chance to test it,

-SiX

singedpiper
01-06-2006, 07:43 PM
sorry angst, I just tested it... it outputs "/" ... i'll retry using absolute paths

singedpiper
01-06-2006, 07:55 PM
I think I have the solution... I was dumb...


when trying absolute paths, I wrote:
$_SERVER['DOCUMENT_ROOT']."music/"

instead of:
$_SERVER['DOCUMENT_ROOT']."/music/"

I'll try editing my code, and get right back to you:.....


ok, it works now... if anyone's still interested, here's my final (ugly) code:



$uploadDir = "files/";
$mydir = "/music/";
$i = 1;
$temp = $uploadDir;


$simplename = basename( $_FILES['file']['name']);
while(file_exists($uploadDir.$simplename))
{
$uploadDir = $temp.$i;
$i++;
}
$uploadFile = $uploadDir.$simplename;

if(is_uploaded_file($_FILES['file']['tmp_name']))
{
}
else
{
echo "the file was not uploaded correctly, try again";
exit(0);
}

$uploadPath = $_SERVER['DOCUMENT_ROOT'].$mydir.$uploadFile;
echo $uploadPath;


if(move_uploaded_file($_FILES["file"]["tmp_name"], $uploadPath))
echo "sucessful upload to $preamble.$uploadFile";
else
{
echo "error moving file from ".$_FILES["file"]["tmp_name"]." to $preamble.$uploadFile";
exit(0);
}

angst
01-06-2006, 08:07 PM
not that bad,
just beautify it a little,
tabbing and what not;-)

-SiX



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum