...

View Full Version : Upload script: IE vs Firefox



MnilinM
02-23-2007, 04:22 PM
Ok, so i've got a script that's kind of for a friend to make new posts/update his site easily, and from anywhere.

So i made a script that inserts the content from certain fields into a mysql database. One of these is a filename.

so the script inserts the information from the input type=file that i have, np.

The problem is this:
Firefox will input filename.jpg
IE will input c:\full\friggin\path\to\the\filename.jpg

My question is, should i be putting something in my code that helps IE just input the filename.jpg and not the path?

aedrin
02-23-2007, 04:35 PM
Sure.

Although I don't think you need to be dealing with this information in PHP. The filename in the $_FILES array should not contain the path fo the filename.



if (strpos($filename, "\\") !== false) {
$filename = substr(strrchr($filename, "\\"), 1);
}

marek_mar
02-23-2007, 04:39 PM
Use the $_FILES array.

CFMaBiSmAd
02-23-2007, 04:41 PM
Even though IE shows the full path in the form input field, the ['name'] element of the $_FILES array only contains the actual file name.

MnilinM
02-23-2007, 05:12 PM
Ok, i've got the actual upload script part down, i think.

What about the information being inserted into mysql?

If i submit an image using FireFox, then use another .php to view the contents of that table, it'll pull imagename.jpg

But if i submit an image with IE, then use the other .php script, it pulls c:\full\path\to\imagename.jpg

So what i think is going on is IE will insert the whole path to mysql, fire fox only inserts the file name into mysql.


$date = $_POST['date'];
$title = $_POST['title'];
$thumb = $_POST['thumb'];
$thcom = $_POST['thcom'];
$story = $_POST['story'];
$poster = $_POST['poster'];

$query = "INSERT INTO news (news_datee, news_timee, news_title, news_th_path, news_th_com, news_story, news_poster) VALUES (NOW(), NOW(), '$title', '$thumb', '$thcom', '$story', '$poster')";
mysql_query($query) or die('Error, insert query failed');

mysql_close();

news_th_path is the name of the table where my imagename is being stored... i didn't think really hard when i named it, and i'll probably rename it. My point is, news_th_path shouldn't contain the whole path, just the imagename.

I just don't get why IE is inserting the whole path and firefox isn't.

CFMaBiSmAd
02-23-2007, 05:34 PM
You would need to post your form code as none of the variables you show or insert into the database are a result of the file upload field.

MnilinM
02-23-2007, 05:38 PM
<form method="post">
<table border="1" cellspacing="1" cellpadding="2">
<tr>
<td>Post Title</td>
<td><input name="title" type="text" size="100" id="title"></td>
</tr>
<tr>
<td>Thumbnail Image</td>
<td><input name="thumb" type="file" size="100" id="thumb"></td>
</tr>
<tr>
<td>Thumbnail Comment</td>
<td><input name="thcom" type="text" size="100" id="thcom"></td>
</tr>
<tr>
<td>Article Text</td>
<td><textarea name="story" rows="20" cols="125" type="text" id="story"></textarea></td>
</tr>
<tr>
<td>Name</td>
<td><input name="poster" type="text" size="100" id="poster"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td><input name="add" type="submit" id="add" value="Add New Post"></td>
</tr>
</table>
</form>

Fumigator
02-23-2007, 05:48 PM
Are you actually uploading the file, or just using the entry field type="file" as an easy way of entering a filename?

Up to this point we've all been assuming you are uploading a file and then storing the filename in your table, but your form isn't correct for uploading.

Just in case you are doing a file upload, you need to do like this:

http://us2.php.net/manual/en/features.file-upload.php

CFMaBiSmAd
02-23-2007, 05:54 PM
My thoughts exactly.

If you are actually uploading, your <form ... tag will need the following -

enctype="multipart/form-data"

Once you do this, the $_POST['thumb'] will go away and your problem will be solved, because you will then need to use the proper $_FILES... ['name'] element in your insert query.

MnilinM
02-23-2007, 06:04 PM
it's always something simple with me, haha.

Ok, so, if i want both functions to occur at the same time?

For instance if i want to insert the filename from the type=file into mysql with the rest of the form, then upload the file, would adding the enctype enable that?

this is what i have:


$date = $_POST['date'];
$title = $_POST['title'];
$thumb = $_POST['thumb'];
$thcom = $_POST['thcom'];
$story = $_POST['story'];
$poster = $_POST['poster'];

$query = "INSERT INTO news (news_datee, news_timee, news_title, news_th_path, news_th_com, news_story, news_poster) VALUES (NOW(), NOW(), '$title', '$thumb', '$thcom', '$story', '$post er')";
mysql_query($query) or die('Error, insert query failed');

mysql_close();

$target_path = "images1/";
$target_path = $target_path . basename($_FILES['thumb']['name']);

// start upload
if(move_uploaded_file($_FILES['thumb']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['thumb']['name']).
" has been uploaded.<br>";
}
else{
echo "There was an error uploading the file!<br>";
}
// end upload

MnilinM
02-23-2007, 07:28 PM
ok, so...

If i add the enctype code to my form, it will upload the file, but it won't add the filename to mysql...

I had a thought, should i be finding a way to use one variable/reference for both the sql insertion and the upload?

If so, which should i use? would something like


$_POST['thumb']['name']

work?

then i can use a variable like


$thumbname = $_POST['thumb']['name']


in both the sql insertion and the upload script?

Fumigator
02-23-2007, 08:47 PM
If you have the upload process working, then inserting a row into a MySQL table using the value of $_FILES['thumb']['name'] shouldn't require any special $_POST variable-- just use $_FILES['thumb']['name'] in your insert query.

MnilinM
02-23-2007, 09:07 PM
excellent! we're up and running.

Thanks a ton to everyone who puts up with the newb questions.

It's extremely appreciated!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum