Page 1 of 1
post method upload question
Posted: Sun Sep 15, 2002 9:07 am
by gijs
Hi,
for several days I tried to get a binary file into a database tabel.
I did finally succeed.
I encountered some problems. Reading up on it, on the Post method upload documentation and the comments did'nt make me any wiser.
Not solved yet:
a) if I use in the form <enctype="multipart/form-data"> nothing happens.
I read that this might be due to the use of a firewall, so I shut it down but ... nothing happens.
b) But apparently without <enctype>, the $_FILES['userfile']['size'] etc... aren't passed, so further control is not possible.
I use following configuration: windows 2000, Apache 2+, PHP 4.2+
Suggestions anybody,
Thanks in advance,
Gijs
Posted: Sun Sep 15, 2002 9:49 am
by hob_goblin
try using
<form enctype="multipart/form-data" method="post" action="url.php">
Posted: Sun Sep 15, 2002 1:09 pm
by gijs
Thanks for the reply hob_goblin,
Tried it.
No result.
Gijs
Posted: Sun Sep 15, 2002 1:54 pm
by dusty
post some of the code you're using
Posted: Sun Sep 15, 2002 11:42 pm
by gijs
Hi,
Let me give you fits the code that is working without <entcype> and then explaining what I want to build in.
Table:
ID | FileName | MimeType | Description | FileData
INT| VARCHAR | VARCHAR | VARCHAR | LONGBLOB
Code in uploadform.php:
Code: Select all
<form action="upload.php" method="POST">
<input type="hidden" name="action" value="ulfile">
<p>Upload File:<br />
<input type="file" name="uploadfile" /><p>
<p>File Description:<br /><input type="text" name="desc" maxlenght="255" /></p>
<p><input type="submit" name"go" value="Upload" /></p>
</form>
Code in upload.php:
Code: Select all
// checking the action status
if (isset($_POSTї'action']) && isset($_POSTї'uploadfile'])) {
switch ($_POSTї'action']) {
case 'ulfile':
//open file for binary reading ("rb")
$tempfile = fopen($_POSTї'uploadfile'],"rb");
// read the entire file into memory using php's
// filesize function to get the filesize
$filedata = fread($tempfile,filesize($_POSTї'uploadfile']));
// prepare for database insert by adding backslashes
// before special characters
$filedata = addslashes($filedata);
// create the sql query
$sql = "INSERT INTO filestore SET
Description='".$_POSTї'desc']."',
FileData='$filedata'";
// perform the insert
$ok = @mysql_query($sql);
if(!$ok) die("Database error storing file: " . mysql_error());
exit();
//stopping the action
break;
default :
// Not yet defined
echo "The default value for the switch statement";
}
} else {
echo "variables not set";
}
mysql_close();
What I like to add are the following things:
1) Controlling if the file is realy un upload at the beginning of the 'case' with something like:
Code: Select all
// Bail out if the file isn't really an upload
if(!is_uploaded_file($_FILESї'uploadfile'])) die("This is not an uploaded file!");
2) Changing the sql_query to insert FileName and Mimetype into the tabel using $_FILES with something like :
Code: Select all
FileName='".$_FILESї'uploadfile']ї'name']."'
If I read this in the documentation correctly it can only work if you use <enctype> to pass the variables for use of $_FILES.
Hopes this sheds some light into the darkness
Thanks in advance,
Gijs