Code: |
<form method="post" action="uploadprocess.php" enctype="multipart/form-data"> <input type="file" name="file1" size="20"> <input type="submit" name="submit" value="submit"> </form> |
Code: |
<? // Upload processor script // At this point your script would determine what storage server to connect to // I'm just going to hardcode it here $Storage_IP = "172.21.5.100"; $Storage_Port = 3306; $Storage_User = "root"; $Storage_Passwd = "secret"; $Storage_DB = "storage1"; $connectto = $Storage_IP . ":" . $Storage_Port; if (!$linkid = @mysql_connect($connectto, $Storage_User, $Storage_Passwd)) { die("Unable to connect to storage server!"); } if (!mysql_select_db($Storage_DB, $linkid)) { die("Unable to connect to storage database!"); } // Init values - these are used incase you want to upload multiple files, you just // add them to the source form as file1, file2, file3, etc. $STARTFILE = 1; $ONFILE = "file" . $STARTFILE; while (isset($HTTP_POST_FILES["$ONFILE"])) { // Try! $SrcPathFile = $HTTP_POST_FILES["$ONFILE"]["tmp_name"]; $SrcFileType = $HTTP_POST_FILES["$ONFILE"]["type"]; $DstFileName = $HTTP_POST_FILES["$ONFILE"]["name"]; clearstatcache(); $time = filemtime($SrcPathFile); $storedate = date("Y-m-d H:i:s", $time); // File Processing if (file_exists($SrcPathFile)) { // Insert into file table $SQL = "insert into file (datatype, name, size, filedate) values ('"; $SQL .= $SrcFileType . "', '" . $DstFileName . "', " . filesize($SrcPathFile); $SQL .= ", '" . $storedate . "')"; if (!$RES = mysql_query($SQL, $linkid)) { die("Failure on insert to file table!"); } $fileid = mysql_insert_id($linkid); // Insert into the filedata table $fp = fopen($SrcPathFile, "rb"); while (!feof($fp)) { // Make the data mysql insert safe $binarydata = addslashes(fread($fp, 65535)); $SQL = "insert into filedata (masterid, filedata) values ("; $SQL .= $fileid . ", '" . $binarydata . "')"; if (!mysql_query($SQL, $linkid)) { die("Failure to insert binary inode data row!"); } } fclose($fp); } $STARTFILE ++; $ONFILE = "file" . $STARTFILE; } echo "Upload Complete"; ?> |
Code: |
<? // Download script.. streams data from a mysql database, thru the webserver to a client browser if (isset($_GET["id"])) { $Storage_IP = "172.21.5.100"; $Storage_Port = 3306; $Storage_User = "root"; $Storage_Passwd = "secret"; $Storage_DB = "storage1"; $connectto = $Storage_IP . ":" . $Storage_Port; if (!$linkid = @mysql_connect($connectto, $Storage_User, $Storage_Passwd)) { die("Unable to connect to storage server!"); } if (!mysql_select_db($Storage_DB, $linkid)) { die("Unable to connect to storage database!"); } $nodelist = array(); // Pull file meta-data $SQL = "select * from file where id = " . $_GET["id"]; if (!$RES = mysql_query($SQL, $linkid)) { die("Failure to retrive file metadata"); } if (mysql_num_rows($RES) != 1) { die("Not a valid file id!"); } $FileObj = mysql_fetch_object($RES); // Pull the list of file inodes $SQL = "SELECT id FROM filedata WHERE masterid = " . $_GET["id"] . " order by id"; if (!$RES = mysql_query($SQL, $linkid)) { die("Failure to retrive list of file inodes"); } while ($CUR = mysql_fetch_object($RES)) { $nodelist[] = $CUR->id; } // Send down the header to the client Header ( "Content-Type: $FileObj->datatype" ); Header ( "Content-Length: " . $FileObj->size ); Header ( "Content-Disposition: attachment; filename=$FileObj->name" ); // Loop thru and stream the nodes 1 by 1 for ($Z = 0 ; $Z < count($nodelist) ; $Z++) { $SQL = "select filedata from filedata where id = " . $nodelist[$Z]; if (!$RESX = mysql_query($SQL, $linkid)) { die("Failure to retrive file node data"); } $DataObj = mysql_fetch_object($RESX); echo $DataObj->filedata; } } ?> |