diff --git a/data.json b/data.json new file mode 100644 index 0000000..e8f77bf --- /dev/null +++ b/data.json @@ -0,0 +1 @@ +{"data":{"filename":"gsdgsd.zip","checklist_item_id":2,"date_uploaded":1624687288,"filesize":1751670,"checksum":"6e924cbe6993cba96b229521b4fd1faf6754cb84"}} \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..e1f0159 --- /dev/null +++ b/index.php @@ -0,0 +1,26 @@ + + + + + PHP File Upload + + + %s', $_SESSION['message']); + unset($_SESSION['message']); + } + ?> +
+
+ Upload a File: + +
+ + +
+ + diff --git a/upload.php b/upload.php new file mode 100644 index 0000000..a50c5f2 --- /dev/null +++ b/upload.php @@ -0,0 +1,85 @@ + $_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.
'; + $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");