Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    221
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question relative path from upload_tmp_dir root folder?

    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?

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try this:
    $_SERVER['DOCUMENT_ROOT']

    -SiX

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    221
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    well,
    for only a relative path, can you just use this? "music/"

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    221
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm,
    ok,, best I could come up with is this:

    PHP Code:

    <?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

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    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 ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    just wondering,
    did this function work?
    I never had a chance to test it,

    -SiX

  • #9
    Regular Coder
    Join Date
    Jan 2005
    Posts
    221
    Thanks
    2
    Thanked 0 Times in 0 Posts
    sorry angst, I just tested it... it outputs "/" ... i'll retry using absolute paths

  • #10
    Regular Coder
    Join Date
    Jan 2005
    Posts
    221
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:

    PHP 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);


  • #11
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    not that bad,
    just beautify it a little,
    tabbing and what not;-)

    -SiX


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •