samedi 25 juin 2016

PHP uploading multiple files and save their path into a single row in DB

i been looking around and didnt found a solution that fits my needs. so lets cut to the chase. SCENARIO: User will fill some text forms, the input of those forms will be stored into a database table (the text input are working and stores into database just fine), in the same form the user will upload 7 files and the paths of the files will be stored in the same table in diferent columns say image1 image2 image3 etc, but in the same row, so when the id of that row is needed the file paths will be retrieved also but im unable to build the query to insert the paths into database, ive managed store only one file and store its path into the database but im struggling with the multiple files, i just dont know how to check if the for loop has finished and how to build the query to insert all upladed file's paths into a single row in their respective columns. Server is running PHP and postgreSQL thanks in advance and hope you can help me! <?php include '../include/dbase.php'; $result = ''; if (isset($_FILES['upload'])) { $nombre = $_FILES['upload']['name']; $ntemporal = $_FILES['upload']['tmp_name']; $tipo = $_FILES['upload']['type']; $tamaño = $_FILES['upload']['size']; $error = $_FILES['upload']['error']; $file_name_all = ''; for($i=0; $i<count($_FILES['upload']['name']); $i++) { $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; if ($tmpFilePath != "") { $path = "../imagenes/propiedades/"; $name = $_FILES['upload']['name'][$i]; $size = $_FILES['upload']['size'][$i]; list($txt, $ext) = explode(".", $name); $file= time().substr(str_replace(" ", "_", $txt), 0); $info = pathinfo($file); $filename = $file.".".$ext; if(move_uploaded_file($_FILES['upload'['tmp_name'][$i], $path.$filename)) { //my guess is an if check here to see if for loop has finished all the uploads, and if returns true a try and catch with the sql query to upload only the path of each file the row containing all other user input text. } } } } ?>

Aucun commentaire:

Enregistrer un commentaire