tomdchi Posted September 20, 2008 Share Posted September 20, 2008 I want the ability to upload docs tied to a client. I have been able to put a form into clientsummary.tpl that will do this into a table called upload. The problem I am having is getting the userid to pass through to the php script. I have tried a bunch of trial and error but no luck. The files do upload fine but there is no userid in the userid field on the db. Can someone take a look and hopefully tell me what I am missing. Still a noob but learning:-) Thanks, Tom Here is the form that I have inserted into clientsummary.tpl: <form method="post" enctype="multipart/form-data" action="https://mydomain.com/resident/upload.php"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> <input name="userid" style="height: 22px" type="hidden" value="{$clientdetails.userid}"></td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> The contents of upload.php file: <?php include("dbconnect.php"); include("includes/functions.php"); if(isset($_POST['upload'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $userid = $_POST['userid']['userid']; // i have tried many variations for the above variable $userid but I think its wrong $fp = fopen($tmpName, 'r'); $content = fread($fp, $fileSize); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = "INSERT INTO upload (userid, name, size, type, content ) ". "VALUES ('$userid', '$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> 0 Quote Link to comment Share on other sites More sharing options...
tomdchi Posted September 21, 2008 Author Share Posted September 21, 2008 Now I need to be able to download these. The script I have picks up the first document in the database for the client but not others and opens up a box to open or save. I want it to bring up a new window displaying all filenames for the given userid with a hyperlink. Can anyone help with this? Here is what I am using now: I access the file below from the admin with a link: download.php?userid={$clientsdetails.userid} <?php if(isset($_GET['userid'])) { include("../dbconnect.php"); include("../includes/functions.php"); $userid = $_GET['userid']; $query = "SELECT name, type, size, content FROM upload WHERE userid = '$userid'"; $result = mysql_query($query) or die('Error, query failed'); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-Disposition: attachment; filename=$name"); header("Content-length: $size"); header("Content-type: $type"); echo $content; exit; } ?> <html> <head> <title>Download File From MySQL</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php include("../dbconnect.php"); include("../includes/functions.php"); $query = "SELECT * upload WHERE userid = '$userid'"; $result = mysql_query($query) or die('Error, query failed'); if(mysql_num_rows($result) == 0) { echo "Database is empty <br>"; } else { while(list($userid, $name) = mysql_fetch_array($result)) { ?> <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br> <? } } ?> </body> </html> What I did to make upload function in admin: Create table: CREATE TABLE upload ( id INT NOT NULL AUTO_INCREMENT, userid INT NOT NULL, name VARCHAR(30) NOT NULL, type VARCHAR(30) NOT NULL, size INT NOT NULL, content MEDIUMBLOB NOT NULL, PRIMARY KEY(id) ); Insert this form into the clientsummary.tpl located at admin/templates/clientsummary.tpl: <form method="post" enctype="multipart/form-data" action="upload.php"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <a href="download.php?userid={$clientsdetails.userid}">view documents</a></td> <td width="80"> </td> </tr> <tr> <td width="246"> <input type="hidden" name="userid" value="{$clientsdetails.userid}"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> then put the contents of this into a file named upload.php and put it into the admin directory. <?php include("../dbconnect.php"); include("../includes/functions.php"); $userID = addslashes($_POST['userid']); if(!empty($_POST['upload'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, $fileSize); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = "INSERT INTO upload (userid, name, size, type, content ) ". "VALUES ('$userID', '$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; echo "User ID: $userID <br />"; } ?> The echo statements where a debugging tool I was using to make sure that the proper values where being sent to the db. I plan to take them out when everything is set the way I want them. 0 Quote Link to comment Share on other sites More sharing options...
tomdchi Posted September 24, 2008 Author Share Posted September 24, 2008 This has been solved and I have posted files here: http://forum.whmcs.com/showthread.php?t=14398 if anyone needs this. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.