Results 1 to 6 of 6

Thread: [RESOLVED] path to filename different between browsers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Resolved [RESOLVED] path to filename different between browsers

    hi all.. I'm trying to open a file from a location in user's computer, using a form with <input type=file ..>, and I put the path into $filename.

    There's no problem when I try it using Internet Explorer. For example, if the path is "D:\Jim\text.txt" then that's exactly what $filename contains.

    But when I try using Firefox and Opera, $filename contains only "text.txt", the path is not included. How can it be different using different browser? What should I do so that it can also work with Firefox and Opera?

    Thank you very much.

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: path to filename different between browsers

    Are you uploading the file or just want a filename? Do you have any code already.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Re: path to filename different between browsers

    I'm trying to read the file and insert them into a database, one item per line.

    here's the code in the html file:
    VB Code:
    1. <form method="post" action="import.php">
    2. Import from file: <input type="file" name="filename" size="75">
    3. <input type="submit" value="GO">
    4. </form>

    and here's the code in the php file:
    VB Code:
    1. if (!db_connect()):
    2.     echo "<p align=\"center\"><font color=\"red\"><b>Cannot connect to the database, please try again later.</b></font></p>";
    3.     exit;
    4. else:
    5.     if ($filename):
    6.         @  $fp = fopen($filename, 'r');
    7.         if (!$fp)
    8.         {
    9.         echo "<p align=\"center\"><font color=\"red\"><strong>File Not Found!</strong></font></p>";
    10.         exit;
    11.         }
    12.  
    13.         $i = 0;
    14.         while (!feof($fp))
    15.         {
    16.         $item = fgets($fp);
    17.         if (trim($url)):
    18.             $query = "INSERT INTO items (description) VALUES ('$item')";
    19.             $result = mysql_query($query);
    20.  
    21.             if (!$result):
    22.                 echo "<p align=\"center\"><font color=\"red\"><strong>Error occured. Please try again later.</strong></font></p>";
    23.             else:
    24.                 $i++;
    25.             endif;
    26.         endif;
    27.         }
    28.         fclose($fp);
    29.     endif;
    30.     echo "<p align=\"center\"><font color=\"red\"><strong>$i items added.</strong></font></p>";
    31. endif;

    Now, it works without any problem in Internet Explorer, but using Opera and FireFox, it always get to the "File Not Found"
    part in the code. When I tried to echo $filename, in IE it shows the full path (like "D:\jim\test.txt"), but using Opera
    and FireFox the $filename only shows "test.txt" without the full path. That's why it gets to the "File Not Found" part
    in the code.

    Please help me, I want to make my website compatible with most browsers. Thanks

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: path to filename different between browsers

    Ok you seem to be getting confused. It looks like your trying to open the file from PHP which is on your computer. This cannot be done, PHP is server side, and the security risks are just mind boggeling if you could. What you need to do is read the file once it has been uploaded.

    http://uk.php.net/manual/en/features.file-upload.php
    http://uk.php.net/manual/en/reserved...ariables.files


    This is something similar to what you are trying to do but your code was a bit ambiguous.
    PHP Code:
    <?
    if(is_uploaded_file($_FILES['fupload']['tmp_name']))
    {
        $fp = fopen($_FILES['fupload']['tmp_name'],"r");
        while(!feof($fp))
        {
            $item .= fgets($fp,1024);
        }
        if (trim($url))
        {
            $query = "INSERT INTO items (description) VALUES ('$item')";
            $result = mysql_query($query);

            if (!$result)
            {
                echo "<p align=\"center\"><font color=\"red\"><strong>Error occured. Please try again later.</strong></font></p>";
            }
            else
            {
                $i++;
            }
        }
        
    }
    ?>
    <html>
        <head>
        </head>
        <body>
        <form enctype="multipart/form-data" method="post">
        <input type="file" name="fupload">
        <input type="submit" value="upload!">
        </form>
        </body>
    </html>

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Re: path to filename different between browsers

    thanks John, it works now.

    but one more question, does the uploaded file remains on the server or deleted automatically? because I don't want it to stay on the server.

    thanks

  6. #6
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: path to filename different between browsers

    PHP will delete the file for you so you dont need to worry about it. Im not sure when it deletes the file but i tihnk it could be when the session expires.

Posting Permissions

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



Click Here to Expand Forum to Full Width