note 27787 added to features.file-upload

From: Date: Wed, 18 Dec 2002 18:29:25 +0000
Subject: note 27787 added to features.file-upload
Groups: php.notes 
Request: Send a blank email to php-notes+get-41075@lists.php.net to get a copy of this message
Here's a full example of how to upload a file and add it to a MySQL database create file "upload.htm" to select and upload a file <html> <body> <FORM ENCTYPE="multipart/form-data" ACTION="upload.php" METHOD=POST> <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="65000"> Send this file: <INPUT NAME="userfile" TYPE="file"> <INPUT TYPE="submit" VALUE="Upload file"> </FORM> </body> </html> create file "upload.php" to add the file to the database (assuming that you are adding the file to an existing record with a BLOB field named "picture"): <? if (is_uploaded_file($userfile)) { copy($userfile, $HTTP_POST_FILES['userfile']['name']); } else { echo "error. File: '$userfile'."; } $filename=$HTTP_POST_FILES['userfile']['name']; $fd = fopen ($filename, "r"); $contents = fread ($fd, filesize($filename)); fclose($fd); $escaped_contents=mysql_escape_string($contents); // Connecting, selecting database $link = mysql_connect("dbhost", "dbuser", "dbpassword") or die("Could not connect"); mysql_select_db("databasename") or die("Could not select database"); // Performing SQL query; assuming that the "picture" is a BLOB field $query = "UPDATE tablename SET picture='$escaped_contents' WHERE id=1"; $result = mysql_query($query) or die("Query failed"); // Closing connection mysql_close($link); ?> create "view.php" to view the image (assuming that you uploaded a GIF-formatted file): <? // Connecting, selecting database $link = mysql_connect("dbhost", "dbuser", "dbpassword") or die("Could not connect"); #print "Connected successfully"; mysql_select_db("databasename") or die("Could not select database"); header(" Content-Type: image/gif"); header(" Content-Disposition: inline"); $sql = "SELECT picture FROM tablename WHERE id=1"; $result = mysql_query($sql); $row = mysql_fetch_row($result); $image = $row[0]; echo $image; ?> And finally: this is how you can use the image in a HTML result file <img src="view.php"> good luck, Maarten Malaise -- http://www.php.net/manual/en/features.file-upload.php http://master.php.net/manage/user-notes.php?action=edit+27787 http://master.php.net/manage/user-notes.php?action=delete+27787 http://master.php.net/manage/user-notes.php?action=reject+27787

« previous php.notes (#41075) next »