11<?php
22$ upload_dir = __DIR__ . '/uploads/ ' ;
3+
4+ if (isset ($ _FILES ["fileUpload " ])) {
5+
6+ // Get file path
7+ $ target_file = $ upload_dir . basename ($ _FILES ["fileUpload " ]["name " ]);
8+
9+ $ saved = move_uploaded_file ($ _FILES ["fileUpload " ]["tmp_name " ], $ target_file );
10+
11+ $ data = array ("Saved " => $ saved ,"Mango " =>95 , "Cherry " =>120 , "Kiwi " =>100 , "Orange " =>55 );
12+
13+ header ('Content-Type: application/json ' );
14+ echo json_encode ($ data );
15+ exit ();
16+
17+ }
318?>
19+ <!doctype html>
20+ <html lang="en">
21+ <head>
22+ <meta charset="UTF-8">
23+ <meta name="viewport"
24+ content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
25+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
26+ <link rel="stylesheet" href="dist/main.css">
27+ <title>A Web exp.!</title>
28+ </head>
29+ <body class="content-fluid">
30+ <header>
31+
32+ </header>
433<div>
534
635 <form action="" method="post" enctype="multipart/form-data" class="mb-3">
736 <h3 class="text-center mb-5">Upload File in PHP 8</h3>
837 <div class="custom-file">
938 <input type="file" name="fileUpload" class="custom-file-input" id="fileUpload">
1039 <label class="custom-file-label" for="chooseFile">Select file</label>
40+ <section id="custom-file-upload-progress">
41+ <div id="progress-bar">
42+ <div id="progress">Progress</div>
43+ <div id="percent">50%</div>
44+ </div>
45+ </section>
1146 </div>
1247 <button type="button" name="ajax_upload" class="btn btn-primary btn-block mt-4" onclick="saveDocumentToS3()">
1348 Ajax upload
1449 </button>
1550
16- </form><script>
51+ <!-- <button type="button" name="select-file" id="select-file">-->
52+ <!-- Select file-->
53+ <!-- </button>-->
54+
55+ </form>
56+ <script src="dist/main.bundle.js"></script>
57+ <script>
1758
1859 async function saveDocumentToS3() {
1960 const currentUrl = window.location.href
2061 let formData = new FormData()
2162 formData.append("fileUpload", fileUpload.files[0])
22- const uploadRequest = await fetch(currentUrl, {
63+ const response = await fetch(currentUrl, {
2364 method: "POST",
2465 body: formData
2566 })
2667
68+ if (!response.ok) {
69+ throw new Error(`HTTP error! status: ${response.status}`);
70+ }
2771
28- const data = await uploadRequest .json()
72+ const data = await response .json()
2973
74+ console.log(data)
3075
31- }
32-
33- </script>
3476
3577
36- </div>
37- <?php
38- if (isset ($ _FILES ["fileUpload " ])) {
3978
40- // Get file path
41- $ target_file = $ upload_dir . basename ($ _FILES ["fileUpload " ]["name " ]);
79+ }
4280
43- $ saved = move_uploaded_file ( $ _FILES [ " fileUpload " ][ " tmp_name " ], $ target_file );
81+ </script>
4482
45- $ data = array ("saved " => $ saved ,"a " => "Apple " , "b " => "Ball " , "c " => "Cat " );
4683
47- header ("Content-Type: application/json " );
48- echo json_encode ($ data );
49- exit ();
84+ </div>
85+ <footer>
86+ Copyright@2023 FlairCore.com
87+ </footer>
5088
51- }
89+ </body>
90+ </html>
91+ <?php
0 commit comments