hi i manage to upload 1 file and save it to the server. how can i add more button to upload a file and save it to mysql? i want is after the user upload a file theres another a button to upload again another a file how can i code it?

here is my code in uploading 1 file

index.php

<form action="add_file.php" method="post" enctype="multipart/form-data">
        <input type="file" name="uploaded_file"><br>
        <input type="submit" value="Upload file">
    </form>

add_file.php

<?php

if(isset($_FILES['uploaded_file'])) {
    
    if($_FILES['uploaded_file']['error'] == 0) {
       
        $dbLink = new mysqli('localhost', 'root', '', 'sampleupload');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }
 
        
        $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
        $size = intval($_FILES['uploaded_file']['size']);
 
        
        $query = "
            INSERT INTO `file` (
                `name`, `mime`, `size`, `data`, `created`
            )
            VALUES (
                '{$name}', '{$mime}', {$size}, '{$data}', NOW()
            )";
 
       
        $result = $dbLink->query($query);
 
     
        if($result) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }
 
   
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}
 

echo '<p>Click <a href="index.php">here</a> to go back</p>';
?>

thank you :)

Recommended Answers

All 9 Replies

Erm, just put more than one file-elements in your HTML-script. the very simplest way of doing so is to make the name of the fields an array:

<form action="add_file.php" method="post" enctype="multipart/form-data">
        <input type="file" name="uploaded_file[0]"><br>
        <input type="file" name="uploaded_file[1]"><br>
        <input type="submit" value="Upload file">
    </form>

In your PHP-file you just have to add a loop that controls the values of each given field. Eg:

for($c=0;$c<8;$c++){
if(isset($_FILES['uploaded_file['.$c.']'])) {
 
    if($_FILES['uploaded_file['.$c.']']['error'] == 0) {
...

That's all. Hope it helps.

where should i end the loop? thanks for your help its a great thought.

sir where should i end the loop?

at the end of your code:

<?php
for($c=0;$c<8;$c++){
  if(isset($_FILES['uploaded_file['.$c.']'])) {
    if($_FILES['uploaded_file['.$c.']']['error'] == 0) {
      //put all your current code here
    }
  }
}//end of loop

echo "Click here to go back";
?>

That's it. Haven't tested it, but it's not hard work to update your code.

hi mate i update my code in this

<?php
  $dbLink = new mysqli('localhost', 'root', '', 'sampleupload');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }
for($c=0;$c<5;$c++){
if(isset($_FILES['uploaded_file['.$c.']'])) {
 
    if($_FILES['uploaded_file['.$c.']']['error'] == 0) {

        $name = $dbLink->real_escape_string($_FILES['uploaded_file['.$c.']']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded_file['.$c.']']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file['.$c.']']['tmp_name']));
        $size = intval($_FILES['uploaded_file['.$c.']']['size']);
 
        
        $query = "
            INSERT INTO `file` (
                `name`, `mime`, `size`, `data`, `created`
            )
            VALUES (
                '{$name}', '{$mime}', {$size}, '{$data}', NOW()
            )";
 }
	

        $result = mysql_query($query);
}
      
 
   
  
}

if($result) {
            echo 'Success! Your file was successfully added!';
}
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }


echo '<p>Click <a href="index.php">here</a> to go back</p>';

?>

but still same error hmmm

My suggestion is to implement this whole process onto one file. That way once it is finished it's still on the same page and they can upload another file without having to click a back button.

Change your submit button to:

<input type="submit" name="upload_file" value="Upload file">

and then at the top of your document

if ($_POST['upload_file'])
{
   // Start the uploading process
}

I hope this helps! Good luck!

i hope anyone can help me to solve my problem :(

bump help :(

I remember i wrote you a working script few days ago.

Multi upload php script.

;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.