Why Files are not uploading to database


#1

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
06%20PM


#2

You need to check for errors, you have nothing that checks you db stuff - there is nothing like a LOB type ^^

I’d also highly recommend not storing blobs in the database. A relational databases power is to sort and search data, you can’t do anything with blobs, they just take up space. Store the file name in the database and the blob itself on the server.


#3

Hi Jim, thanks for responding. I just checked the table for errors and it says its ok.

This is my table structure.
48%20PM

As far as the LOB type you were referring to. What should I change this to PDO::PARAM_LOB)?


#4

I meant in the code. You do not check for any errors from either the prepare or execute function calls.

I still highly advise you to change it to not store BLOBS in the database, it’s just generally not done.


#5

Thanks, when I run the whole thing there aren’t any errors. I don’t know how to check for errors in the function or how to impliment that, I am really new to this.


#6

Hey guys could anyone help me insert this block of code into my index.php file? It restricts the file type

<?php

$sys = mime_content_type($_FILES[“fileToUpload”][“tmp_name”]);

if($sys == ‘application/x-zip’ || $sys == ‘application/msword’){

echo ’ allowed’;

}else{

echo ‘not allowed’;

}

?>