3 changed files with 112 additions and 0 deletions
Split View
Diff Options
-
1data.json
-
26index.php
-
85upload.php
@ -0,0 +1 @@ |
|||
{"data":{"filename":"gsdgsd.zip","checklist_item_id":2,"date_uploaded":1624687288,"filesize":1751670,"checksum":"6e924cbe6993cba96b229521b4fd1faf6754cb84"}} |
|||
@ -0,0 +1,26 @@ |
|||
<?php |
|||
session_start(); |
|||
?>
|
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head> |
|||
<title>PHP File Upload</title> |
|||
</head> |
|||
<body> |
|||
<?php |
|||
if (isset($_SESSION['message']) && $_SESSION['message']) |
|||
{ |
|||
printf('<b>%s</b>', $_SESSION['message']); |
|||
unset($_SESSION['message']); |
|||
} |
|||
?>
|
|||
<form method="POST" action="upload.php" enctype="multipart/form-data"> |
|||
<div> |
|||
<span>Upload a File:</span> |
|||
<input type="file" name="uploadedFile" /> |
|||
</div> |
|||
|
|||
<input type="submit" name="uploadBtn" value="Upload" /> |
|||
</form> |
|||
</body> |
|||
</html> |
|||
@ -0,0 +1,85 @@ |
|||
<?php |
|||
session_start(); |
|||
|
|||
$message = ''; |
|||
if (isset($_POST['uploadBtn']) && $_POST['uploadBtn'] == 'Upload') |
|||
{ |
|||
if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] === UPLOAD_ERR_OK) |
|||
{ |
|||
// get details of the uploaded file
|
|||
$fileTmpPath = $_FILES['uploadedFile']['tmp_name']; |
|||
$fileName = $_FILES['uploadedFile']['name']; |
|||
$fileSize = $_FILES['uploadedFile']['size']; |
|||
$fileType = $_FILES['uploadedFile']['type']; |
|||
$fileNameCmps = explode(".", $fileName); |
|||
$fileExtension = strtolower(end($fileNameCmps)); |
|||
|
|||
$data = array( |
|||
'filename' => $_FILES['uploadedFile']['name'], |
|||
'checklist_item_id' => 2, // temporary checklist_item_id
|
|||
'date_uploaded' => time($_FILES['uploadedFile']['tmp_name']), |
|||
'filesize' => $_FILES['uploadedFile']['size'], |
|||
'checksum' => sha1_file($_FILES['uploadedFile']['tmp_name']), |
|||
); |
|||
|
|||
|
|||
// sanitize file-name
|
|||
$newFileName = $fileName; |
|||
//$checksum = md5(time() . $fileName) . '.' . $fileExtension
|
|||
// check if file has one of the following extensions
|
|||
$allowedfileExtensions = array('zip'); |
|||
|
|||
if (in_array($fileExtension, $allowedfileExtensions)) |
|||
{ |
|||
// directory in which the uploaded file will be moved
|
|||
$uploadFileDir = './uploaded_files/'; |
|||
$dest_path = $uploadFileDir . $newFileName; |
|||
|
|||
if(move_uploaded_file($fileTmpPath, $dest_path)) |
|||
{ |
|||
$message ='File is successfully uploaded.'; |
|||
} |
|||
else |
|||
{ |
|||
$message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.'; |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
$message = 'Upload failed. Allowed file types: ' . implode(',', $allowedfileExtensions); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
$message = 'There is some error in the file upload. Please check the following error.<br>'; |
|||
$message .= 'Error:' . $_FILES['uploadedFile']['error']; |
|||
} |
|||
} |
|||
// Read JSON file
|
|||
$json = file_get_contents('./data.json'); |
|||
|
|||
// encode array to json
|
|||
$jsone = json_encode(array('data' => $data)); |
|||
|
|||
//Decode JSON
|
|||
$json_data = json_decode($json,true); |
|||
//Decode JSONe
|
|||
$jsone_data = json_decode($jsone,true); |
|||
|
|||
print_r($json_data); |
|||
print_r($jsone_data); |
|||
$jsa =serialize($json_data); |
|||
$jsb =serialize($jsone_data); |
|||
if($jsa["data"]["name"] == $jsb["0"]["name"]){ |
|||
array_replace($json_data,$jsone_data); |
|||
$jsonDatap = json_encode($jsone_data); |
|||
file_put_contents('./data.json', $jsonDatap); |
|||
} |
|||
else |
|||
{ |
|||
array_push($jsone_data, $data); |
|||
$jsonDataf = json_encode($jsone_data); |
|||
file_append('./data.json', $jsonDataf); |
|||
} |
|||
$_SESSION['message'] = $message; |
|||
header("Location: index.php"); |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue