PhpDude
PhpDude

Reputation: 1600

File path insert multiple rows MySQL and Php

I am currently able to perform an upload of multiple images and then inserting the file paths into a row in the database. Ideally I need to find a way to upload these files paths as separate entries with their own ID. The reason why is because the image paths I am inserting are to be bound to a task which is inserted into a separate table.

function upload_file_new_task(){

global $db;

if(isset($_POST['create'])) {
    $path = "../uploads/";
    for ($i=0; $i<count($_FILES['files']['name']); $i++) {
        $ext = explode('.', basename( $_FILES['files']['name'][$i]));
        $path = $path . md5(uniqid()) . "." . $ext[count($ext)-1]; 
        move_uploaded_file($_FILES['files']['tmp_name'][$i], $path);
    }

    $sql = "INSERT INTO upload_data (`image`) VALUES ('$path');";
    $res = mysqli_query($db,$sql);
    echo "<p>Post Created $date</p>";
}

}

So theimage is uploaded into the /uploads folder, the path is then loaded into the database as a single row with an id in the ID field and the path(s) are loaded into the image field.

Upvotes: 0

Views: 781

Answers (1)

Praveen Kumar
Praveen Kumar

Reputation: 2408

Something like

function upload_file_new_task()
{    
global $db;

if(isset($_POST['create'])) 
{
 $path = "../uploads/";
 for ($i=0; $i<count($_FILES['files']['name']); $i++) 
 {
   $ext = pathinfo($_FILES['files']['name'][$i], PATHINFO_EXTENSION);
   $path1 = $path . md5(uniqid()) . "." . $ext; 
   move_uploaded_file($_FILES['files']['tmp_name'][$i], $path1);
   $sql = "INSERT INTO upload_data (`image`) VALUES ('$path1');";
   $res = mysqli_query($db,$sql);
 }
 echo "<p>Post Created $date</p>";
}

}

Upvotes: 1

Related Questions