-
Notifications
You must be signed in to change notification settings - Fork 2
/
uploadProcess.php
170 lines (143 loc) · 6.58 KB
/
uploadProcess.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<html>
<head>
<title>
||upload status||
</title>
</head>
<body>
<?php
ini_set('max_execution_time', 3000);
session_start();
function getLabel($fname){
$target_url = "http://suzukinakamura.pythonanywhere.com/upload";
//$fname = 'test.jpg';
$cfile = new CURLFile(realpath($fname));
$post = array (
'image' => $cfile
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec ($ch);
if ($result === FALSE) {
curl_close ($ch);
return "Error sending";
}else{
curl_close ($ch);
return $result;
}
}
if(isset($_GET["debug"])){
echo 'debug mode';
}else {
# code...
if($_SESSION['login']===1)
{
$name=$_SESSION['username'];
$fname=trim($_POST['name']);
$question=$_POST['quest'];
$answer=$_POST['ans'];
/*
* connetion setup with the database
*/
require './module/class/Database.php';
/*
* check for user folder is not exist create new one
*/
$dir="./drive/";
$user=$name;
$mix = "/mix";
$base_dir=$dir . $user;
$new_dir=$dir . $user . $mix;
if(!file_exists($new_dir))
{
mkdir($new_dir, 0700);
echo 'folder created<br/>Now you can upload your file<br>';
}
/*
* checking for the file whether it exist or not
* if it exist give error and request for new file name
* else upload the file
*/
$target_dir = $new_dir . '/';
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$fileName=basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file is a actual or fake file
if(isset($_POST["submit"])) {
$check = filesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is valid - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not in valid format.";
$uploadOk = 0;
}
}
/*
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
*/
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
$new_folder = getLabel($target_file);
echo '<br>Target File name: ' . $target_file;
echo '<br>new folder label: ' . $new_folder;
$new_path = $base_dir . '/Images/' . $new_folder . '/' . $fileName;
echo "<br>new path: $new_path";
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $new_path)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
/*
* when file is uploaded save the corresponding data in the database
*/
$db = new Database();
if($db->connect()){
$query="INSERT INTO details(fname,question,answer,username,filepath) VALUES
('" .$fname. "','".$question."','" .$answer. "','" .$name. "','" .$fileName. "')";
$db->sql($query);
$res = $db->getResult();
print_r($res);
$db->disconnect();
echo '<script language="javascript">';
echo 'alert("uploaded to => ' . $new_folder . '");';
echo 'window.location = "upload.php"';
echo '</script>';
//header('Location:upload.php');
}else{
echo 'Database Connection error';
}
}
else
{
echo 'no permission to view this page';
}
}
?>
<br>
<span style="float:center;color:red;"><a href="access.php">Logout</a></span>
</body>
</html>