Jump to content

Recommended Posts

Hi Guys!

 

I've currently got a file upload system running where a file is uploaded then the next page places the file and takes the filename and inserts this into a database. Simple.

 

I've now got to make a new section of the site which handles multiple file uploads...

 

I've managaed to find a Javascript which adds multiple files on one page and a submit button. This then uploads (i assume) and now i'm stuck as how to handle the multiple files uploaded. Below is the scripts used. I'd be so so grateful if someone could help me out with this, i'm building a gallery section and it's all built apart from this one last item! Thanks so much ppl.

 

Kind Regards, Chris

 

<form enctype="multipart/form-data" action="gallery-write-photos.php?albumid=<? echo"$albumid"; ?>&photographerid=<? echo"photographerid"; ?>" method = "post">

<input id="my_file_element" type="file" name="file_1" >
<input type="submit">
</form>
Files:
<!-- This is where the output will appear -->
<div id="files_list"></div>
<script>
<!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->
var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 60 );
<!-- Pass in the file element -->
multi_selector.addElement( document.getElementById( 'my_file_element' ) );
</script>

 

function MultiSelector( list_target, max ){this.list_target = list_target;this.count = 0;this.id = 0;if( max ){this.max = max;} else {this.max = -1;};this.addElement = function( element ){if( element.tagName == 'INPUT' && element.type == 'file' ){element.name = 'file_' + this.id++;element.multi_selector = this;element.onchange = function(){var new_element = document.createElement( 'input' );new_element.type = 'file';this.parentNode.insertBefore( new_element, this );this.multi_selector.addElement( new_element );this.multi_selector.addListRow( this );this.style.position = 'absolute';this.style.left = '-1000px';};if( this.max != -1 && this.count >= this.max ){element.disabled = true;};this.count++;this.current_element = element;} else {alert( 'Error: not a file input element' );};};this.addListRow = function( element ){var new_row = document.createElement( 'div' );var new_row_button = document.createElement( 'input' );new_row_button.type = 'button';new_row_button.value = 'Delete';new_row.element = element;new_row_button.onclick= function(){this.parentNode.element.parentNode.removeChild( this.parentNode.element );this.parentNode.parentNode.removeChild( this.parentNode );this.parentNode.element.multi_selector.count--;this.parentNode.element.multi_selector.current_element.disabled = false;return false;};new_row.innerHTML = element.value;new_row.appendChild( new_row_button );this.list_target.appendChild( new_row );};};

 

Link to comment
https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/
Share on other sites

Ahh awesome!

 

Only question, how do i get the value for $value? The number of files uploaded? I dont know how to find this!

Here is the upload code i use...

 

$user = $_GET['user'];





if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["size"] < 320000000000)) {
    
      $newname = dirname(__FILE__).'/netdog/userpics/'.$filename;




      if (!file_exists($newname)) {
        
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
          
	  
	  
	  
	   mysql_connect ("localhost","##","##");
		@mysql_select_db("##") or die ("unable to connect");




		$query = " UPDATE USER SET image = '$filename' WHERE user='$user' ";

		mysql_query($query);

		mysql_close();


		echo "<meta http-equiv=\"refresh\" content=\"0;URL=profile.php?user=$user\">";




        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "<meta http-equiv=\"refresh\" content=\"0;URL=netdog-error-image.php?user=$user\">";
  }
} else {
echo "Error: No file uploaded";
}
?>

FILES:Array
(
    [file_3] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [file_2] => Array
        (
            [name] => 03022009679.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpWk7TET
            [error] => 0
            [size] => 325854
        )

    [file_1] => Array
        (
            [name] => 10022009684.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpKZXhT2
            [error] => 0
            [size] => 534468
        )

    [file_0] => Array
        (
            [name] => 16022009691.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpsKwTsF
            [error] => 0
            [size] => 489147
        )

)

ok i'll try and work through it...

 

I've tried using the code from the example, to just get the files at least saved to the right folder, but it's not liking the name...

 

<?php
foreach ($_FILES["file_"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["file_"]["tmp_name"][$key];
        $name = $_FILES["file_"]["name"][$key];
        move_uploaded_file($tmp_name, "gallery/images/$name");
    }
}
?>

 

Isnt that right? The name in the previous page is set at "file_1" which then obviously goes up one each time...

I think it should be more something like this:

<?php
foreach ($_FILES as $key => $value) {
    if ($_FILES[$key]["error"] == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES[$key]["tmp_name"];
        $name = $_FILES[$key]["name"];
        move_uploaded_file($tmp_name, "gallery/images/$name");
    }
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.