How To Upload File From Users Computer To Database
For you requirements it sounds like you want a separate table for each user. Although I'm non sure if this is the virtually efficient option.
Perhaps you lot could add a field on your table to agree user_id which would reference your users table using foriegn/principal keys. And so you could utilize a SELECT where user_id='blah blah'.
Otherwise to practice it your manner, just create a 'files' table for each user upon registration. Call the table name $user_id.'files' (that variable tin come up from $_POST or your session variables. And so yous tin use $query = "SELECT * from ".$user_id."files;" in your lawmaking that recalls the information.
ryan311 -7 Posting Whiz in Grooming
create userid to your file table to make a unique file to the user.
too i suggest to yous, do not put your file in your dbase information technology will get bigger and hard to load someday/somehow, create a binder to upload all of your files there. delete this code and delete the information entity to your database
$data = $dbLink->real_escape_string(file_get_contents($_FILES ));
try this code.
<?php // Cheque if a file has been uploaded if(isset($_FILES['uploaded_file'])) { // Brand sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { // Connect to the database $dbLink = new mysqli('xxxxx', 'xxxxx', 'xxxxx', 'xxxxx'); if(mysqli_connect_errno()) { dice("MySQL connection failed: ". mysqli_connect_error()); } $target = "images/"; $target = $target . basename( $_FILES['uploaded_file']['proper name']); // Gather all required data $userid = $_SESSION['id']; //login session $proper noun = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); // delete this line $data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name'])); $size = intval($_FILES['uploaded_file']['size']); // Create the SQL query //add userid to your database delete the data entity. $query = " INSERT INTO `file` ( `userid`, `proper noun`, `mime`, `size`, `created` ) VALUES ('{$userid}', '{$name}', '{$mime}', {$size}, NOW() )"; // Execute the query $result = $dbLink->query($query); // Cheque if information technology was successfull if($result) { move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target); echo 'Success! Your file was successfully added!'; } else { echo 'Error! Failed to insert the file' . "<pre>{$dbLink->error}</pre>"; } } else { echo 'An error accured while the file was existence uploaded. ' . 'Error code: '. intval($_FILES['uploaded_file']['error']); } // Close the mysql connection $dbLink->close(); } else { echo 'Error! A file was not sent!'; } // Echo a link back to the main folio echo '<p>Click <a href="index.php">here</a> to go back!</p>'; ?>
ryan311 -7 Posting Whiz in Preparation
i have ane question for yous.
can user download and share the file?
if yous say yeah
here is your code:
<?php // Cheque if a file has been uploaded if(isset($_FILES['uploaded_file'])) { // Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { // Connect to the database $dbLink = new mysqli('xxxxx', 'xxxxx', 'xxxxx', 'xxxxx'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } $userid = $_SESSION['id']; //login session $target = "images/".$userid."_"; $target = $target . basename( $_FILES['uploaded_file']['name']); // Gather all required data $proper noun = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); // delete this line $information = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name'])); $size = intval($_FILES['uploaded_file']['size']); // Create the SQL query //add userid to your database delete the information entity. $query = " INSERT INTO `file` ( `userid`, `name`, `mime`, `size`, `created` ) VALUES ('{$userid}', '{$name}', '{$mime}', {$size}, At present() )"; // Execute the query $result = $dbLink->query($query); // Check if it was successfull if($outcome) { move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target); echo 'Success! Your file was successfully added!'; } else { repeat 'Error! Failed to insert the file' . "<pre>{$dbLink->fault}</pre>"; } } else { repeat 'An error accured while the file was existence uploaded. ' . 'Error code: '. intval($_FILES['uploaded_file']['error']); } // Close the mysql connexion $dbLink->shut(); } else { echo 'Error! A file was not sent!'; } // Repeat a link back to the master page echo '<p>Click <a href="index.php">here</a> to go back!</p>'; ?>
if you say no only add userid to your file.
to view all of his file
<table cellpadding="0" cellspacing="0" border="ane"> <tr> <thursday bgcolor="black" width="150"><p><font color="white">File Proper name</font></p></th> <thursday bgcolor="blackness" width="150"><p><font color="white">Date Uploaded</font></p></th> </tr> <?php $id = $_SESSION['id']; // login session. $sql = "Select * from file where userid='$id'"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($issue)) { ?> <tr> <td><p><font colour="#000099"><?php repeat $row['name']; ?></font></p></td> <td><p><?php echo $row['created']; ?></p></td> </tr> <?php } ?> </Tabular array>
ryan311 -7 Posting Whiz in Training
no, don't rename or delete the id in your file because that is the unique file you need that in downloading or sharing a file. just add userid entity into your file
so hither is the entity of your file table
-id /auto_increment
-userid /userid
-name / file name
-mime / file type
-size / file size
-created
How To Upload File From Users Computer To Database,
Source: https://www.daniweb.com/programming/web-development/threads/318091/uploading-file-by-user-id-in-mysql
Posted by: hawkinsantionne.blogspot.com
0 Response to "How To Upload File From Users Computer To Database"
Post a Comment