Download file with php and save sql path

Does anyone know a good tutorial on how to download a file from php and save the file path on sql server?

+4
source share
3 answers

To upload a file, you need at least an HTML POST form with multipart/form-data . In this case, you put the input type="file" field to view the file and the submit button to submit the form.

 <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit"> </form> 

In upload.php downloaded file is available $_FILES with the field name as the key.

 $file = $_FILES['file']; 

You can get its name as follows:

 $name = $file['name']; 

You need to move it to a permanent place using move_uploaded_file() , otherwise it will be lost:

 $path = "/uploads/" . basename($name); if (move_uploaded_file($file['tmp_name'], $path)) { // Move succeed. } else { // Move failed. Possible duplicate? } 

You can save the path in the database in the usual way:

 $sql = "INSERT INTO file (path) VALUES ('" . mysqli_real_escape_string($path) . "')"; // ... 
+12
source

From http://www.w3schools.com/php/php_file_upload.asp

HTML

 <html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> 

Php

 <?php if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; //<- This is it } } ?> 

Please note that to download a file you need to specify the path to save the file. If you save the file, you already know its path.

+5
source

He asked for a textbook in his question

0
source

Source: https://habr.com/ru/post/1310446/


All Articles