-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
50 lines (42 loc) · 1.63 KB
/
upload.php
File metadata and controls
50 lines (42 loc) · 1.63 KB
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
<?php
ob_start();
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
if(!$_SESSION['valid'])
header('Location: index.php');
if(file_exists('passwd'))
$dict = json_decode(file_get_contents('passwd'), true);
else
$dict = array();
if ($_POST['from_user'] !== $_SESSION['username'] || !array_key_exists($_POST['to_user'], $dict))
return;
if($_FILES['file']['error'] === UPLOAD_ERR_NO_FILE){
http_response_code(422);
echo 'The file does not exist.';
return;
}
if(empty($_FILES['file']) || $_FILES['file']['error'] === UPLOAD_ERR_INI_SIZE || $_FILES['file']['error'] === UPLOAD_ERR_FORM_SIZE || $_FILES['file']['size'] > 11000000){
http_response_code(413);
echo 'File size must be no more than 11 MB.';
return;
}
if(strcmp($_POST['from_user'], $_POST['to_user']) < 0){
$file_dir = 'upload_files/'.$_POST['from_user'].'|'.$_POST['to_user'];
$chat_file_path = 'chat/'.$_POST['from_user'].'|'.$_POST['to_user'];
}
else{
$file_dir = 'upload_files/'.$_POST['to_user'].'|'.$_POST['from_user'];
$chat_file_path = 'chat/'.$_POST['to_user'].'|'.$_POST['from_user'];
}
if(!file_exists($file_dir)){
mkdir($file_dir);
chmod($file_dir, 0711);
}
$name = $file_dir.'/'.hash('sha256', $_FILES['file']['tmp_name']).time();
move_uploaded_file($_FILES['file']['tmp_name'], $name);
chmod($name, 0644);
file_put_contents($chat_file_path, $_POST['from_user'].' '.bin2hex("<a href=.$filedir/$name download=".basename($_FILES['file']['name']).'>'.$_FILES['file']['name'].'</a>')."\n", FILE_APPEND);
file_put_contents('new_message/'.$_POST['to_user'].'/'.$_POST['from_user'], '');
echo '';
?>