-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUploading pictures to DB
150 lines (103 loc) · 4.28 KB
/
Uploading pictures to DB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
Index.php :
<html>
<head>
<title>Upload Picture to Database</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="icon" type="image/png" href="fav.png">
<script>
function process(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("shopList").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","loadList.php",true);
xmlhttp.send();
}
</script>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript" src="abling.js"></script>
</head>
<body onload="process()">
<h1>Upload Picture to Database</h1>
<p>Developed using PHP, JavaScript, jQuery & MySQL</p>
<form name="Image" id="browse" enctype="multipart/form-data" action="upload.php" method="POST">
<input type="file" name="Photo" size="2000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26"><br/>
<label>Name this picture! : </label><input name="newName" id="newName" ><br/>
<INPUT type="submit" id="subm" class="button" name="Submit" value=" Submit " disabled="disabled">
<INPUT type="reset" class="button" value="Cancel"><br/>
</form>
<div id="shopList"></div>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
upload.php copies the picture file from source directory to destination directory, and uploads the picture's location into the database :
<?php
header('Content-type: image/jpeg');
session_start();
require("connect.php");
$uploadDir = 'pic/'; //Image Upload Folder
if(isset($_POST['Submit'])){
$new_name="someone";
if( isset($_POST['newName']) ){
$new_name=mysql_real_escape_string ( strip_tags( $_POST['newName'] ) );
}
$fileName = $_FILES['Photo']['name'];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc()){
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$image = imagecreatefrompng($filePath);
imagepng($image, $filePath);
$query = "INSERT INTO example.products ( Pid, Pname, Powner, Ppic ) VALUES ('7', '$new_name', 'me', '$filePath')";
mysql_query($query) or die('Error, query failed');
header("location: index.php"); exit();
}
?>
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
connect.php connects to the database :
<?php
if ( !@mysql_connect('localhost', 'root', '') || !@mysql_select_db(example) ){
die('Cannot connect');
}
?>
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
loadList.php re-displays the uploaded pictures to the webpage :
<?php
include'connect.php';
header('Content-type: image/jpeg');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$sql="SELECT * FROM example.products";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
$image=$row ['Ppic'];
echo '<ul style="list-style-type:none;display: inline-block;">';
echo '<li id="loadList">';
echo '<img src="'.$image.'" width="150px" height="80px"/>';
echo '</li>';
echo '</ul>';
}
echo '</response>';
?>
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
abling.js controls the behaviour of the buttons on the upload form :
$(document).ready(function(){
$('#browse').change(function(){
$('#subm').removeAttr('disabled');
});
});