Link to home
Start Free TrialLog in
Avatar of Abhishek Dubey
Abhishek Dubey

asked on

I just want to upload 6 pics together but the problem is ---In db , all 5pics take same name of 1st pic (means) (all 6pics goes in upload file but from same name)

when i submit pics
all 6 pics are upload but with same name in db
& in upload file all diffrent 6 pics are uploaded but with same name.


and all other things goes right.
Avatar of Insoftservice inso
Insoftservice inso
Flag of India image

please provide the code spec if possible
Here is my teaching example showing how to upload multiple files.  The "organization" of $_FILES is one of PHP's weak points, and it takes some effort to decipher it.  Once you get that, you'll be OK with the upload process.
<?php // demo/upload_multiple_example.php

/**
 * Demonstrate how to upload a single file in PHP
 *
 * REQUIRED: Man Page References
 * http://php.net/manual/en/reserved.variables.files.php
 * http://php.net/manual/en/features.file-upload.php
 * http://php.net/manual/en/features.file-upload.common-pitfalls.php
 * http://php.net/manual/en/features.file-upload.errors.php
 * http://php.net/manual/en/features.file-upload.multiple.php
 *
 * http://php.net/manual/en/function.move-uploaded-file.php
 *
 * IMPORTANT: If dealing with large files
 * http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
 * http://php.net/manual/en/ini.core.php#ini.post-max-size
 * http://php.net/manual/en/info.configuration.php#ini.max-input-time
 */
error_reporting(E_ALL);

// PHP 5.1+  SEE http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('America/Chicago');

// ESTABLISH THE NAME OF THE DESTINATION FOLDER ('uploads' DIRECTORY)
$uploads = 'storage';
if (!is_dir($uploads))
{
    mkdir($uploads);
}

// ESTABLISH THE BIGGEST FILE SIZE WE WILL ACCEPT - ABOUT 8 MB
$max_file_size = 8 * 1024 * 1024;

// ESTABLISH THE MAXIMUM NUMBER OF FILES WE WILL UPLOAD
$nf = 3;

// ESTABLISH THE KINDS OF FILE EXTENSIONS WE WILL ACCEPT
$file_exts = array
( 'jpg'
, 'gif'
, 'png'
, 'txt'
, 'pdf'
, 'doc'
, 'docx'
)
;

// LIST OF THE ERRORS THAT MAY BE REPORTED IN $_FILES[]["error"] (THERE IS NO #5)
$errors = array
( UPLOAD_ERR_OK         => "Success!"
, UPLOAD_ERR_INI_SIZE   => "The uploaded file exceeds the upload_max_filesize directive in php.ini"
, UPLOAD_ERR_FORM_SIZE  => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
, UPLOAD_ERR_PARTIAL    => "The uploaded file was only partially uploaded"
, UPLOAD_ERR_NO_FILE    => "No file was uploaded"
, 5                     => "UNDEFINED ERROR"
, UPLOAD_ERR_NO_TMP_DIR => "Missing a temporary folder"
, UPLOAD_ERR_CANT_WRITE => "Cannot write file to disk"
, UPLOAD_ERR_EXTENSION  => "A PHP extension stopped the file upload"
)
;


// IF WE HAVE GOT SOMETHING IN $_POST - RUN THE ACTION SCRIPT
if (!empty($_POST))
{
    echo "<h2>Results: File Upload</h2>" . PHP_EOL;

    // ACTIVATE THIS TO SEE WHAT IS COMING THROUGH
    // echo "<pre>"; var_dump($_FILES); var_dump($_POST); echo "</pre>";

    // ITERATE OVER THE CONTENTS OF $_FILES
    foreach ($_FILES as $my_uploaded_file)
    {
        // SKIP OVER EMPTY SPOTS - NOTHING UPLOADED
        $error_code = $my_uploaded_file["error"];
        if ($error_code == UPLOAD_ERR_NO_FILE) continue;

        // SYNTHESIZE THE NEW FILE NAME
        $f_type = explode('.', basename($my_uploaded_file['name']));
        $f_type = end($f_type);
        $f_type = trim(strtolower($f_type));

        $f_name = explode('.', basename($my_uploaded_file['name']));
        $f_name = current($f_name);
        $f_name = trim(strtolower($f_name));

        $my_new_file
        = getcwd()
        . DIRECTORY_SEPARATOR
        . $uploads
        . DIRECTORY_SEPARATOR
        . $f_name
        . '.'
        . $f_type
        ;
        $my_file
        = $uploads
        . DIRECTORY_SEPARATOR
        . $f_name
        . '.'
        . $f_type;

        // OPTIONAL TEST FOR ALLOWABLE EXTENSIONS
        if (!in_array($f_type, $file_exts))
        {
            trigger_error("$f_type Not allowed", E_USER_WARNING);
            continue;
        }

        // IF THERE ARE ERRORS
        if ($error_code != UPLOAD_ERR_OK)
        {
            $error_message = $errors[$error_code];
            trigger_error("Upload error code: $error_code: $error_message", E_USER_WARNING);
            continue;
        }

        // GET THE FILE SIZE
        $file_size = number_format($my_uploaded_file["size"]);

        // IF THE FILE IS NEW (DOES NOT EXIST)
        if (!file_exists($my_new_file))
        {
            // IF THE MOVE FUNCTION WORKED CORRECTLY
            if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
            {
                $upload_success = 1;
            }
            // IF THE MOVE FUNCTION FAILED
            else
            {
                $upload_success = -1;
            }
        }

        // IF THE FILE ALREADY EXISTS
        else
        {
            echo "<br/><b><i>$my_file</i></b> already exists." . PHP_EOL;

            // SHOULD WE OVERWRITE THE FILE? IF NOT
            if (empty($_POST["overwrite"]))
            {
                $upload_success = 0;
            }
            // IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
            else
            {
                $now    = date('Y-m-d\THis');
                $my_bak = $my_new_file . '.' . $now . '.bak';
                if (!copy($my_new_file, $my_bak))
                {
                    trigger_error("Backup Failed for $my_file", E_USER_WARNING);
                }
                if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
                {
                    $upload_success = 2;
                }
                else
                {
                    $upload_success = -1;
                }
            }
        }

        // REPORT OUR SUCCESS OR FAILURE
        if ($upload_success == 2) { echo "<br/>It has been overwritten." . PHP_EOL; }
        if ($upload_success == 1) { echo "<br/><b>$my_file</b> has been saved." . PHP_EOL; }
        if ($upload_success == 0) { echo "<br/><b>It was NOT overwritten.</b>" . PHP_EOL; }
        if ($upload_success < 0)  { echo "<br/><b>ERROR: $my_file NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</b>" . PHP_EOL; }
        if ($upload_success > 0)
        {
            echo "$file_size bytes uploaded." . PHP_EOL;
            if (!chmod ($my_new_file, 0755))
            {
                echo '<br/>chmod(0755) FAILED: fileperms() = ';
                echo substr(sprintf('%o', fileperms($my_new_file)), -4);
            }
            echo '<br/><a target="_blank" href="' . $my_file . '">See the file ' . $my_file . '</a>' . PHP_EOL;
        }
    } // END FOREACH ITERATOR - EACH ITERATION PROCESSES ONE FILE
} // END ACTION SCRIPT


// FORM SCRIPT: CREATE THE INPUT STATEMENTS FOR THE FILES
$inputs = NULL;
for ($n = 0; $n < $nf; $n++)
{
    $inputs .= '<input name="userfile' . $n . '" type="file" size="80" /><br/>' . PHP_EOL;
}

// CREATE THE HTML FORM USING HEREDOC NOTATION
$form = <<<EOF
<h2>Upload from 1 to $nf file(s)</h2>
<!--
    SOME IMPORTANT THINGS TO NOTE ABOUT THIS FORM...
    ENCTYPE= ATTRIBUTE IN THE HTML <FORM> TAG
    MAX_FILE_SIZE HIDDEN CONTROL MUST PRECEDE THE FILE INPUT CONTROLS
    INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN _FILES ARRAY
    ABSENCE OF ACTION= ATTRIBUTE IN <FORM> TAG CAUSES POST TO SAME URL
-->
<form name="UploadForm" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="$max_file_size" />
<p>
Find the file(s) you want to upload and click the "Upload" button below.
</p>

$inputs

<br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <b>overwrite</b> existing files.
<input type="submit" value="Upload" />
</form>
EOF;

echo $form;

Open in new window

Avatar of Abhishek Dubey
Abhishek Dubey

ASKER

model
<!--model-->
<?php
class demomod extends CI_Model
{
    
     public function insrec($t,$d)
   {
      $this->db->insert($t,$d);      
   }
   
   ?>
   
   <!--HTML-FORM-->
   
   
   <?php 
   <form method="post" enctype="multipart/form-data">
<div align="center">
	<table border="1" width="83%" height="513">
		<tr>
			<td colspan="5">
			<p align="center"><font color="#800080" size="5"><i><b>REGISTRATION 
			- FORM</b></i></font></td>
		</tr>
		<tr>
			<td width="31%">
			<p align="center"><b>PIC UPLOAD</b></td>
			<td width="15%"><input type="file" name="file1" size="11"></td>
			<td width="15%"><input type="file" name="file2" size="11"></td>
			<td width="15%"><input type="file" name="file3" size="11"></td>
			<td width="16%">
			<p align="center">DEATILS</td>
		</tr>
		<tr>
			<td width="31%" height="42">
			<p align="center"><b>PIC UPLOAD</b></td>
			<td width="15%" height="42">
			
				<p><input type="file" name="file4" size="11"></p>
				
				
			</td>
			<td width="15%" height="42">
			<input type="file" name="file5" size="11"></td>
			<td width="15%" height="42">
			<input type="file" name="file6" size="11"></td>
			<td width="16%" height="42"><textarea rows="2" name="S1" cols="20"></textarea></td>
		</tr>
		<tr>
			<td width="31%">NAME OF HOSTEL</td>
			<td width="15%"><p><input type="text" name="T2" size="20"></p>
			</form>
			</td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="16%"><textarea rows="2" name="S2" cols="20"></textarea></td>
		</tr>
		<tr>
			<td width="31%">MANAGER NAME</td>
			<td width="15%"><input type="text" name="T3" size="20"></td>
			<td width="15%">MANAGER PH. NO.</td>
			<td width="15%"><input type="text" name="T4" size="20"></td>
			<td width="16%"><textarea rows="2" name="S3" cols="20"></textarea></td>
		</tr>
		<tr>
			<td width="31%">OWNER NAME</td>
			<td width="15%"><input type="text" name="T5" size="20"></td>
			<td width="15%">OWNER PH. NO.</td>
			<td width="15%"><input type="text" name="T6" size="20"></td>
			<td width="16%"><textarea rows="2" name="S4" cols="20"></textarea></td>
		</tr>
		<tr>
			<td width="31%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="16%">&nbsp;</td>
		</tr>
		<tr>
			<td width="31%">TOTAL NO OF ROOMS</td>
			<td width="15%"><input type="text" name="T7" size="7"></td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="16%"><textarea rows="2" name="S5" cols="20"></textarea></td>
		</tr>
		<tr>
			<td width="31%">&nbsp;SINGLE ROOM WITH A.C (L &amp; T) AVILABALE</td>
			<td width="15%"><input type="text" name="T8" size="20"></td>
			<td width="15%">OF RS.(SINGLE A.C)</td>
			<td width="15%"><input type="text" name="T9" size="20"></td>
			<td width="16%"><textarea rows="2" name="S6" cols="20"></textarea></td>
		</tr>
		<tr>
			<td width="31%">SINGLE ROOM WITHOUT A.C (L &amp; T) AVILABALE</td>
			<td width="15%"><input type="text" name="T10" size="20"></td>
			<td width="15%">OF RS. (SINGLE)</td>
			<td width="15%"><input type="text" name="T11" size="20"></td>
			<td width="16%"><textarea rows="2" name="S7" cols="20"></textarea></td>
		</tr>
		<tr>
			<td width="31%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="16%">&nbsp;</td>
		</tr>
		<tr>
			<td width="31%">DOUBLE WITH A.C&nbsp; (L &amp; T) AVILABALE</td>
			<td width="15%"><input type="text" name="T12" size="20"></td>
			<td width="15%">OF RS.(DOUBL A.C)</td>
			<td width="15%"><input type="text" name="T13" size="20"></td>
			<td width="16%"><textarea rows="2" name="S8" cols="20"></textarea></td>
		</tr>
		<tr>
			<td width="31%">DOUBLE WITHOUT A.C&nbsp; (L &amp; T) AVILABALE</td>
			<td width="15%"><input type="text" name="T14" size="20"></td>
			<td width="15%">OF RS.(DOUBL)</td>
			<td width="15%"><input type="text" name="T15" size="20"></td>
			<td width="16%"><textarea rows="2" name="S9" cols="20"></textarea></td>
		</tr>
		<tr>
			<td width="31%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="16%">&nbsp;</td>
		</tr>
		<tr>
			<td width="31%">REGISTERING TIME AND DATE</td>
			<td width="15%"><input type="text" name="T16" size="20"></td>
			<td width="15%">TO TILL</td>
			<td width="15%"><input type="text" name="T17" size="20"></td>
			<td width="16%">&nbsp;</td>
		</tr>
		<tr>
			<td width="31%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="16%">&nbsp;</td>
		</tr>
		<tr>
			<td width="31%">AREA CODE</td>
			<td width="15%"><input type="text" name="T18" size="11"></td>
			<td width="15%">&nbsp;</td>
			<td width="15%">&nbsp;</td>
			<td width="16%">&nbsp;</td>
		</tr>
		<tr>
			<td width="31%" height="33">&nbsp;</td>
			<td width="46%" colspan="3" height="33">
			<p><input type="submit" value="Submit" name="submit">
                            <input type="reset" value="Reset" name="B2"></p>
			</td>
			<td width="16%" height="33">&nbsp;</td>
		</tr>
	</table>
</div>
</form>
</body>
   
   
   ?>

Open in new window

Interesting.  You didn't mention CodeIgniter.  Are you using CodeIgniter?  Asking because the CodeIgniter File Uploading Class does not mention support for multiple file uploads.  You might try this solution (full disclosure: I have not tested this).
https://github.com/stvnthomas/CodeIgniter-Multi-Upload
Yes sir i m using codginator
and i save file (MY_Upload.php) In applications-> library

when i dont save 1st time & write do_multi_upload it gives me error of undefined variable.

but after save your given extension (MY_Upload.php) error will not come

but

problem is as it is.
controller2.php
Since this looks like a data-dependent problem, you may want to visualize the data that is in your variables.  You can use PHP var_dump() to see what is going on inside variables, including the superglobal $_FILES.
Avatar of Ahmed Merghani
I faced same problem couple of years ago.
Try to rename the file input tag names to "files[]".
In controller change
$this->upload->do_multi_upload('file1','file2','file3','file4','file5','file6');

Open in new window

to just:        
$this->upload->do_multi_upload('files');

Open in new window

And
$img1=$d['file_name'];
$img2=$d['file_name'];
$img3=$d['file_name'];
$img4=$d['file_name'];
$img5=$d['file_name'];
$img6=$d['file_name'];

Open in new window

to
$img1=$d['file_name'][0];
$img2=$d['file_name'][1];
$img3=$d['file_name'][2];
$img4=$d['file_name'][3];
$img5=$d['file_name'][4];
$img6=$d['file_name'][5];

Open in new window

Wish this solve your problem.
Just one last thing, I think the html code in your model should be in view.
i tried as u guide me (Ahmed Merghani)

but it giving error
 Uninitialized string offset
from  0 to 5
not solving yet
plz any can?
Sorry but it was long time ago and I am not sure if it is:
$img1=$d['file_name'][0];
$img2=$d['file_name'][1];
$img3=$d['file_name'][2];
$img4=$d['file_name'][3];
$img5=$d['file_name'][4];
$img6=$d['file_name'][5];

Open in new window

Or you need to replace it with:
$img1=$d['files'][0];
$img2=$d['files'][1];
$img3=$d['files'][2];
$img4=$d['files'][3];
$img5=$d['files'][4];
$img6=$d['files'][5];

Open in new window

now this happend


A PHP Error was encountered

Severity: Notice

Message: Undefined index: files

Filename: controllers/democont.php

Line Number: 53
i really really gone frustate from this
bcoz
 i m surviving from this from last atleast 2 months.
I believe we are missing something!!
We can use the pure PHP global variable to work around this issue.
You need to replace this:
$img1=$d['files'][0];
$img2=$d['files'][1];
$img3=$d['files'][2];
$img4=$d['files'][3];
$img5=$d['files'][4];
$img6=$d['files'][5];

Open in new window

With:
$img1=$_FILES['userfile']['name'][0];
$img2=$_FILES['userfile']['name'][1];
$img3=$_FILES['userfile']['name'][2];
$img4=$_FILES['userfile']['name'][3];
$img5=$_FILES['userfile']['name'][4];
$img6=$_FILES['userfile']['name'][5];

Open in new window

Wish this solve the problem.
Also try this.
replace the "imgs" part with this:
$str = "img";
		for($i=1; $i <= 5; $i++){
			$this->upload->do_upload("file".($i));
			$d = $this->upload->data();		
			${$str.$i} = $d['file_name'];
		}

Open in new window

Remember to set the file input names to: file1, file2 ... file5.
WHAT happend of mine.
Database-Error.html
control.php
actually sir i want to say

i m thankfull to you that you  are helping me but

i want to tell a small thing that

please if you can run the particular(only upload code part) code.so this will really help me.bcoz from that you get all error in front of you and or i will get right code from ,

other reasons  may be

i m using coginator version (CodeIgniter_2.1.4)

and i  i save file (MY_Upload.php) In applications-> library as extension.

if you can sir please this may be really helpfull for me.
ASKER CERTIFIED SOLUTION
Avatar of Ahmed Merghani
Ahmed Merghani
Flag of Sudan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
checking sir
OH Yes you did it thank you very much sir.


its my pleasure to follow

you are polite sharp minded person
i wish you will earn all things which you want

really thanks heartly
That is great.
For your comment, first the for loop must be as:
for($i=1; $i <= 6; $i++){

Open in new window

6 not 7.
Then, the input names must 6: file1, file2 ... file6.
Wish this is the missing thing.
sir i accept answer it gives you 500 points if right plz tell me
bcoz by accepting multple solutions it giving marks to others.
REALLY THANKS.
Yes its 500 and you are welcome