-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_file.php
53 lines (50 loc) · 1.69 KB
/
upload_file.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
<?php
session_start();
if (!isset($_SESSION['access_token'])) {
header('Location: login.php');
exit();
}
$email= $_SESSION['email'] ;
$fileExistsFlag = 0;
$fileName = $_FILES['Filename']['name'];
$link = mysqli_connect("localhost","root","","test") or die("Error ".mysqli_error($link));
/*
* Checking whether the file already exists in the destination folder
*/
$query = "SELECT filename FROM content WHERE filename='$fileName'";
$result = $link->query($query) or die("Error : ".mysqli_error($link));
while($row = mysqli_fetch_array($result)) {
if($row['filename'] == $fileName) {
$fileExistsFlag = 1;
}
}
/*
* If file is not present in the destination folder
*/
if($fileExistsFlag == 0) {
$target = "files/";
$fileTarget = $target.$fileName;
$tempFileName = $_FILES["Filename"]["tmp_name"];
$fileDescription = $_POST['Description'];
$result = move_uploaded_file($tempFileName,$fileTarget);
/*
* If file was successfully uploaded in the destination folder
*/
if($result) {
echo "Your file <html><b><i>".$fileName."</i></b></html> has been successfully uploaded";
$query = "INSERT INTO content(filepath,filename,description,email) VALUES ('$fileTarget','$fileName','$fileDescription','$email')";
$link->query($query) or die("Error : ".mysqli_error($link));
}
else {
echo "Sorry !!! There was an error in uploading your file";
}
mysqli_close($link);
}
/*
* If file is already present in the destination folder
*/
else {
echo "File <html><b><i>".$fileName."</i></b></html> already exists in your folder. Please rename the file and try again.";
mysqli_close($link);
}
?>