I am using a form field "file" to get a file to upload. I want to strip out all the path except for the filename and insert that into the database as my file.

Is there a way to do this

    I think this should work
    (i'm assuming the filename you want is the current file)

    basename($_SERVER['PHP_SELF']);

    -sijis

      ok but where is my variable if my variable I want to extract is called userfile

      basename($userfile['PHP_SELF']);

      is that correct?

      D

        <? $userFile = basename($_SESSION['PHP_SELF']);?>

          ok great I can get it to work for a unix path but what do I do for a windows path i.e.

          C:\Documents and Settings\drabe\Desktop\OnCallMaintenanceTool-QuoteID#136.doc

          any ideas?

          D

            I'm trying to get the filename of a http_referer. I.E Strip the accompyaning URL and leave index.php.

            I know how to get the filename of the current file:

            $userFile = basename($_SERVER['PHP_SELF']); 
            

            But when I replace 'PHP_SELF' with HTTP_REFERER, I get the error:

            Notice: Undefined index: HTTP_REFERER in /var/www/httpdocs/test.php on line 3
            How can I do this?
            

              To XyDon1

              $filename = "C:\\Documents and Settings\\drabe\\Desktop\\OnCallMaintenanceTool-QuoteID#136.doc"
              
              // Change the backslashes to forward slashes before you do the basename() function
              $filename = str_replace("\\\", "/", $filename);
              
              // Then run the basename() function
              $filename = basename($filename);
              

                tom_s,

                i'm sure you are trying this:

                $userFile = basename($_SERVER['HTTP_REFERER']);
                

                A possible reason its giving you that error is if you go directly to that test.php file. Doing so, http_referer will not be set and that's maybe why you're getting that error. Now if you are getting to your test.php file via a link then http_referer should be set or at least have a value.

                I can't think of any other reason's why you would have a problem.

                btw, other predefined variables can be found here:
                http://us3.php.net/reserved.variables

                --sijis

                  xydon1,

                  i wonder if using pathinfo() could also be useful.

                  ex:

                  $path = "C:\\Documents and Settings\\drabe\\Desktop\\OnCallMaintenanceTool-QuoteID#136.doc"; // uses 2 backslashes
                  
                  $filename = $path["basename"] . $path["extension"];
                  
                  

                  i think that should work.
                  TheDefender's solution seems useful too.

                  function:
                  http://us3.php.net/pathinfo

                    $path = pathinfo("C:\\Documents and Settings\\drabe\\Desktop\\OnCallMaintenanceTool-QuoteID#136.doc"); // uses 2 backslashes 
                    
                    $filename = $path["basename"];
                    echo $filename;
                    

                    You wouldn't need the $path['extension'] variable, since the extension is included in the basename.

                    Also, you need to wrap pathinfo() around the file's name, but it seems to work fine as well.

                      haha..
                      i didn't actually test the code i posted.. dumb me.

                      i totally forgot to use the function i suggested to use... that's silly.

                      i appreciate the revision.

                      "there are many ways to do things."

                      --sijis

                        Write a Reply...