Upload and download pdf-file to mySQL
I write a webapp for a soccer team. They want to save the contracts of the players to the contact form.
So I wrote a test page to upload a file into a blobmedium column and one for download the pdf-file. When I try to download the file Adobe Reader say not an unknow file type. The uploaded file was 275kB and the downloaded 1kB. When I try the same with a word file. Than, when I download him, I see in the doc:
Notice: Undefined index: data in c:\wamp\www\Test\OpenFIles.php on line: 32.
The 'file' column in my table is a 'mediumblob'.
This is my code to upload:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
//kijk of er iets is upgeload naar de server
if ($_FILES['userfile']['size'] > 0) {
if ($_FILES['userfile']['error'] == 0) {
// databank openen
$dbGerry = new mysqli("localhost", "xxx", "xxx", "test");
// Check connection
if (mysqli_connect_errno())
die("Failed to connect to MySQL: " . mysql_connect_error());
//Verzamel de data
$fileName = $dbGerry->real_escape_string($_FILES['userfile']['name']);
$data = $dbGerry->real_escape_string(file_get_contents($_FILES['userfile']['tmp_name'])); //Sz naam die het bestand tijdelijk heeft gekregen op de server
$fileSize = intval($_FILES['userfile']['size']); //nodig om te kunnen downloaden
$fileType = $dbGerry->real_escape_string($_FILES['userfile']['type']); //nodig om te kunnen downloaden
// insert-query
$query = "INSERT INTO filetable (name, size, type, file, created ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$data', NOW())";
// query uitvoeren
$result = $dbGerry->query($query);
//goed uitgevoerd?
if($result) {
echo "<br>Het bestand '$fileName' is goed bewaard</br>";
}
else {
echo 'Error! Niet gelukt '
. "<pre>{$dbLink->error}</pre>";
}
$dbGerry->close();
}
else {
echo "Er is een fout opgetreden bij het opladen van het bestand. "
. "Error code: ". intval($_FILES['userfile']['error']);
}
}
?>
//kijk of er iets is upgeload naar de server
if ($_FILES['userfile']['size'] > 0) {
if ($_FILES['userfile']['error'] == 0) {
// databank openen
$dbGerry = new mysqli("localhost", "xxx", "xxx", "test");
// Check connection
if (mysqli_connect_errno())
die("Failed to connect to MySQL: " . mysql_connect_error());
//Verzamel de data
$fileName = $dbGerry->real_escape_string($_FILES['userfile']['name']);
$data = $dbGerry->real_escape_string(file_get_contents($_FILES['userfile']['tmp_name'])); //Sz naam die het bestand tijdelijk heeft gekregen op de server
$fileSize = intval($_FILES['userfile']['size']); //nodig om te kunnen downloaden
$fileType = $dbGerry->real_escape_string($_FILES['userfile']['type']); //nodig om te kunnen downloaden
// insert-query
$query = "INSERT INTO filetable (name, size, type, file, created ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$data', NOW())";
// query uitvoeren
$result = $dbGerry->query($query);
//goed uitgevoerd?
if($result) {
echo "<br>Het bestand '$fileName' is goed bewaard</br>";
}
else {
echo 'Error! Niet gelukt '
. "<pre>{$dbLink->error}</pre>";
}
$dbGerry->close();
}
else {
echo "Er is een fout opgetreden bij het opladen van het bestand. "
. "Error code: ". intval($_FILES['userfile']['error']);
}
}
?>
My download page is:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
if(isset($_GET['id'])) {
//Neem het id nummer
$id =intval($_GET['id']);
// databank openen
$dbGerry = new mysqli("localhost", "xxx", "xxx", "test");
// Check connection
if (mysqli_connect_errno())
die("Failed to connect to MySQL: " . mysql_connect_error());
//select query
$query = "SELECT name, type, size, file " .
"FROM filetable WHERE id = '$id'";
//query uitvoeren
$result = $dbGerry->query($query);
// gegevens ophalen
$row = mysqli_fetch_assoc($result);
//vul de headers
header("Content-length: " . $row['size']);
header("Content-type: " . $row['type']);
header("Content-Disposition: attachment; filename=" . $row['name']);
//toon het bestand
echo $content;
@mysqli_free_res($result);
$dbGerry->close();
}
?>
if(isset($_GET['id'])) {
//Neem het id nummer
$id =intval($_GET['id']);
// databank openen
$dbGerry = new mysqli("localhost", "xxx", "xxx", "test");
// Check connection
if (mysqli_connect_errno())
die("Failed to connect to MySQL: " . mysql_connect_error());
//select query
$query = "SELECT name, type, size, file " .
"FROM filetable WHERE id = '$id'";
//query uitvoeren
$result = $dbGerry->query($query);
// gegevens ophalen
$row = mysqli_fetch_assoc($result);
//vul de headers
header("Content-length: " . $row['size']);
header("Content-type: " . $row['type']);
header("Content-Disposition: attachment; filename=" . $row['name']);
//toon het bestand
echo $content;
@mysqli_free_res($result);
$dbGerry->close();
}
?>
Gewijzigd op 13/10/2014 09:56:21 door Philippe De Weweire
Er zijn nog geen reacties op dit bericht.