spikypunker Posted May 6, 2009 Share Posted May 6, 2009 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 );};}; Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/ Share on other sites More sharing options...
jackpf Posted May 6, 2009 Share Posted May 6, 2009 Just put your current file upload script in this loop: foreach($_FILES as $key => $value) { //your stuff here //but use $_FILES[$key] rather than $_FILES['formElement'] } Something like that. Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-827410 Share on other sites More sharing options...
spikypunker Posted May 6, 2009 Author Share Posted May 6, 2009 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-827424 Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2009 Share Posted May 6, 2009 What exact format is the multi-uploaded files in? What does the following code give - echo "<pre>"; echo "FILES:"; print_r($_FILES); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-827433 Share on other sites More sharing options...
spikypunker Posted May 6, 2009 Author Share Posted May 6, 2009 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-827447 Share on other sites More sharing options...
spikypunker Posted May 6, 2009 Author Share Posted May 6, 2009 ...and the exact file format is .jpg. Can anyone help me piece this last bit together? :oD Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-827564 Share on other sites More sharing options...
jackpf Posted May 6, 2009 Share Posted May 6, 2009 So what's not working? Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-827575 Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2009 Share Posted May 6, 2009 You might want to read Example #3 at this link on how to handle multiple file uploads - http://www.php.net/manual/en/features.file-upload.post-method.php Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-827579 Share on other sites More sharing options...
spikypunker Posted May 6, 2009 Author Share Posted May 6, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-827680 Share on other sites More sharing options...
jackpf Posted May 6, 2009 Share Posted May 6, 2009 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"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-827731 Share on other sites More sharing options...
spikypunker Posted May 8, 2009 Author Share Posted May 8, 2009 woooooo!! I solved it from this, cheers guys! Quote Link to comment https://forums.phpfreaks.com/topic/157074-solved-handling-multiple-files/#findComment-829416 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.