0

I want to upload a file with a submit button : https://www.w3schools.com/php/php_file_upload.asp

I want to mix this tuto with a function who upload my file on Sharepoint.

My code

                        <form action="" method="post" enctype="multipart/form-data">
                        <input type="file" name="fileToUpload" id="fileToUpload" accept=".xlsx">
                        <input type="submit"  value="Upload File" onClick="sendRequest" name="submit">
                        </form>
                        <?php

                            use Microsoft\Graph\Graph;
                            use Microsoft\Graph\Model;
                            $guzzle = new \GuzzleHttp\Client();

                            function sendRequest(){

                            $file = $_FILES["fileToUpload"]["name"];

                            
                            $accessToken = '$accessToken';

                            $graph = new Graph();
                            $graph->setAccessToken($accessToken);
                            
                            $user = $graph->createCollectionRequest("PUT", "/sites/site-id/drive/root:/Test/folder/$file:/content")
                            ->upload($file);
                            }

                            ?>

But i have an error 405, i don't know if is the best solution ? Maybe i need to make a javascript SDK instead a PHP SDK ?

Thank you for yours answers.

Jedguy
  • 13
  • 5
  • You can't set a PHP function (which is server side) as a onClick event in HTML/JS (which is client side). – M. Eriksson May 05 '22 at 09:27
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – M. Eriksson May 05 '22 at 09:27
  • So if i set ```
    ``` i can reload my view and use my function on the same time
    – Jedguy May 05 '22 at 09:39
  • If you set `action="viewname.php"` on your form and you submit it, the browser will submit the form to that page (and redirect the browser as well) where you can do what ever you need. If you want to upload/submit the form in the background (without redirecting the browser to that page), you need to read up on making Ajax requests (using JavaScript) – M. Eriksson May 05 '22 at 09:51
  • It's not problematic to relaod my view but the result is an error 405. – Jedguy May 05 '22 at 10:01
  • Which result? When you try to submit to viewname.php or in your Graph API request? If it's the API request, you need to check their documentation. If it's the form submit that causes 405 when trying to load viewname.php, then there must be something in your application or server config that's causing it – M. Eriksson May 05 '22 at 10:04
  • When i submit to viewname.php, i will put a path with viewname.php – Jedguy May 05 '22 at 12:09

0 Answers0