Hey guys, I am unable to upload files to my database for some reason. Its not throwing any errors. I am very new to this and tried adding action=“index.php” into the form, but still not working . Any help would be appreciated.
index.php
> <html> > <head> > <meta charset="utf-8"/> > <title>Upload PDF & Word files to Database</title> > </head> > <body> > <?php > $dbh = new PDO("mysql:host=localhost;dbname=database","user","root"); > if(isset($_POST['btn'])){ > $name = $_FILES['myfile']['name']; > $mime = $_FILES['myfile']['type']; > $data = file_get_contents($_FILES['myfile']['tmp_name']); > $stmt = $dbh->prepare("insert into myblob values('',?,?,?)"); > $stmt->bindParam(1,$name); > $stmt->bindParam(2,$mime); > $stmt->bindParam(3,$data, PDO::PARAM_LOB); > $stmt->execute(); > } > ?> > <form method="post" enctype="multipart/form-data" action="index.php"></form> > <input type="file" name="myfile"/> > <input name="btn" type="submit" value="Upload"> > </form> > <p></p> > <ol> > <?php > $stat = $dbh->prepare("select * from myblob"); > $stat->execute(); > while($row = $stat->fetch()){ > echo "<li><a href='view.php?id=".$row['id']."' target='_blank'>".$row['name']."</a></li>"; > } > > > ?> > > </ol> > > </body> > </html>
view.php
> <?php
> $dbh = new PDO("mysql:host=localhost;dbname=database","user","root");
> $id = isset($_GET['id'])? $_GET['id'] : "";
> $stat = $dbh->prepare("select * from myblob where id=?");
> $stat->bindParam(1,$id);
> $stat->execute();
> $row = $stat->fetch();
> header("Content-Type:".$row['mime']);
> echo $row['data'];
> echo '<img src="data:image/jpeg;base64,'.base64_encode($row['data']).'"/>';
Table in Database