|
-
Feb 23rd, 2006, 09:18 PM
#1
Thread Starter
Addicted Member
[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.
-
Feb 23rd, 2006, 09:49 PM
#2
<?="Moderator"?>
Re: path to filename different between browsers
Are you uploading the file or just want a filename? Do you have any code already.
-
Feb 24th, 2006, 06:48 PM
#3
Thread Starter
Addicted Member
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:
<form method="post" action="import.php">
Import from file: <input type="file" name="filename" size="75">
<input type="submit" value="GO">
</form>
and here's the code in the php file:
VB Code:
if (!db_connect()):
echo "<p align=\"center\"><font color=\"red\"><b>Cannot connect to the database, please try again later.</b></font></p>";
exit;
else:
if ($filename):
@ $fp = fopen($filename, 'r');
if (!$fp)
{
echo "<p align=\"center\"><font color=\"red\"><strong>File Not Found!</strong></font></p>";
exit;
}
$i = 0;
while (!feof($fp))
{
$item = fgets($fp);
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++;
endif;
endif;
}
fclose($fp);
endif;
echo "<p align=\"center\"><font color=\"red\"><strong>$i items added.</strong></font></p>";
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
-
Feb 25th, 2006, 08:37 AM
#4
<?="Moderator"?>
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>
-
Feb 28th, 2006, 06:32 AM
#5
Thread Starter
Addicted Member
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
-
Feb 28th, 2006, 11:33 AM
#6
<?="Moderator"?>
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|