3 changed files with 110 additions and 0 deletions
Split View
Diff Options
-
1data.json
-
95fileUploadScript.php
-
14index.html
@ -0,0 +1 @@ |
|||
{"data":{"filename":"angadi-catalog-008.zip","checklist_item_id":2,"date_uploaded":"2021-06-25 11:14:36","filesize":4096,"checksum":"da39a3ee5e6b4b0d3255bfef95601890afd80709"}} |
|||
@ -0,0 +1,95 @@ |
|||
<?php |
|||
$currentDirectory = getcwd(); |
|||
$uploadDirectory = "/uploads/"; |
|||
|
|||
$errors = []; // Store errors here
|
|||
|
|||
$fileExtensionsAllowed = ['zip']; // These will be the only file extensions allowed
|
|||
|
|||
$fileName = $_FILES['the_file']['name']; |
|||
$fileSize = $_FILES['the_file']['size']; |
|||
$fileTmpName = $_FILES['the_file']['tmp_name']; |
|||
$fileType = $_FILES['the_file']['type']; |
|||
$fileExtension = strtolower(end(explode('.',$fileName))); |
|||
|
|||
$uploadPath = $currentDirectory . $uploadDirectory . basename($fileName); |
|||
|
|||
if (isset($_POST['submit'])) { |
|||
|
|||
if (! in_array($fileExtension,$fileExtensionsAllowed)) { |
|||
$errors[] = "This file extension is not allowed. Please upload a . zip file"; |
|||
} |
|||
|
|||
if ($fileSize > 4000000) { |
|||
$errors[] = "File exceeds maximum size (4MB)"; |
|||
} |
|||
|
|||
if (empty($errors)) { |
|||
$didUpload = move_uploaded_file($fileTmpName, $uploadPath); |
|||
|
|||
if ($didUpload) { |
|||
echo "The file " . basename($fileName) . " has been uploaded"; |
|||
} else { |
|||
echo "An error occurred. Please contact the administrator."; |
|||
} |
|||
} else { |
|||
foreach ($errors as $error) { |
|||
echo $error . "These are the errors" . "\n"; |
|||
} |
|||
} |
|||
|
|||
} |
|||
$data = array( |
|||
'filename' => $_FILES["the_file"]["name"], |
|||
'checklist_item_id' => 2, // temporary checklist_item_id
|
|||
'date_uploaded' => date('Y-m-d h:i:s'), |
|||
'filesize' => filesize($currentDirectory['the_file']['temp_name']), |
|||
'checksum' => sha1_file($currentDirectory['the_file']['temp_name']), |
|||
); |
|||
|
|||
|
|||
move_uploaded_file($_FILES["the_file"]["tmp_name"], $output_dir.$fileName); |
|||
$ret[]= $fileName; |
|||
echo "</br>"; |
|||
echo json_encode($data['filename']); |
|||
echo "</br>"; |
|||
echo json_encode($data['checksum']); |
|||
echo "</br>"; |
|||
echo json_encode($data['filesize']); |
|||
echo "</br>"; |
|||
echo json_encode($data['date_uploaded']); |
|||
|
|||
// 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["0"]["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_put_contents('./data.json', $jsonDataf); |
|||
} |
|||
//write json to file
|
|||
//if (file_put_contents("data.json", $jsone))
|
|||
// echo "\r\n JSON file created successfully...";
|
|||
//else
|
|||
// echo "\r\n Oops! Error creating json file...";
|
|||
|
|||
|
|||
?>
|
|||
@ -0,0 +1,14 @@ |
|||
<!DOCTYPE html> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta charset="UTF-8"> |
|||
<title>PHP File Upload</title> |
|||
</head> |
|||
<body> |
|||
<form action="fileUploadScript.php" method="post" enctype="multipart/form-data"> |
|||
Upload a zip File: |
|||
<input type="file" name="the_file" id="fileToUpload"> |
|||
<input type="submit" name="submit" value="Start Upload"> |
|||
</form> |
|||
</body> |
|||
</html> |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue