post method upload question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gijs
Forum Commoner
Posts: 53
Joined: Wed Aug 28, 2002 4:05 am
Location: Belgium

post method upload question

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

try using

<form enctype="multipart/form-data" method="post" action="url.php">
User avatar
gijs
Forum Commoner
Posts: 53
Joined: Wed Aug 28, 2002 4:05 am
Location: Belgium

Post by gijs »

Thanks for the reply hob_goblin,

Tried it.
No result.
:(

Gijs
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

post some of the code you're using
User avatar
gijs
Forum Commoner
Posts: 53
Joined: Wed Aug 28, 2002 4:05 am
Location: Belgium

Post 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&#1111;'action']) && isset($_POST&#1111;'uploadfile'])) &#123;
switch ($_POST&#1111;'action']) &#123;
					
			case 'ulfile':
			
			//open file for binary reading ("rb")
	 		$tempfile = fopen($_POST&#1111;'uploadfile'],"rb");

			// read the entire file into memory using php's
			// filesize function to get the filesize
	   		$filedata = fread($tempfile,filesize($_POST&#1111;'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&#1111;'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";

			&#125;
&#125; else &#123;
echo "variables not set";
&#125; 
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&#1111;'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&#1111;'uploadfile']&#1111;'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 :roll:

Thanks in advance,

Gijs
Post Reply