Jump to content

PHP reading multiple form file variables generated by JS


richk

Recommended Posts

Hello all, new to the forums was advised to sign up here.

 

Here is the problem:

 

The system I am building has JS dynamically adding elements to a form these elements are of type "file" and suffixes are numerically incremented on the prefix "file_". 

 

When it comes to the PHP I have the following:

 

if ('file_$imgcount' != NULL) { //'file_$imgcount'works in loop but will not work below 
$target = "images/";
$target = $target . basename( $_FILES['file_0']['name']);
$pic=($_FILES['file_0']['name']);
mysql_query("INSERT INTO images (listing_id, imgpath) VALUES ($lis, '$pic')");
if(move_uploaded_file($_FILES['file_0']['tmp_name'], $target)) {
	session_start();//temp test ok
	echo "clientID test = ". $_SESSION['client'];
	echo "The file ". basename( $_FILES['uploadedfile']). " has been uploaded, and your information has been added to the directory"; 
}
else {
//...
}
}

 

Initially I had all calls to the file tested by just adding a single file so used 'file_0'.  Now that i wanted to throw the whole thing in a loop  it only understands the 'file_$imgcount' var in the loop conditions, not in the functions.

 

Hope someone can help, please let me know if I missed out any information.

 

Rich

Oh I forgot to mention it also works if named 'file_$imgcount' on the following line:

 

$pic=($_FILES['file_$imgcount']['name']);//works here

 

I have no idea why it does not work in the other lines.  If i try use the variable in the name on the following line alone:

 

$target = $target . basename( $_FILES['file_$imgcount']['name']);

 

Dreamweaver 8.0 gives these errors:

Warning: move_uploaded_file(images/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/projbid/public_html/inserts.php on line 30

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpmwFEW6' to 'images/' in /home/projbid/public_html/inserts.php on line 30

 

Appreciate any help.

 

Thanks

 

Rich

Thank you for taking a look

 

Here is the form:

 

<form enctype="multipart/form-data" action="inserts.php" onsubmit="editor.toggle();" method="post">
<div>Images:</div>
<div><input id="my_file_element" type="file" /></div>
<div id="files_list"></div>

 

Here is where the dynamic elements are added(js):

 

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( 'Please add images of only .jpg .png format' );
	};
};

 

new object is called:

 

var multi_selector = new MultiSelector( document.getElementById( 'files_list' ));
multi_selector.addElement( document.getElementById( 'my_file_element' ) );

 

and the PHP:

 

if ('file_$imgcount' != NULL) { 
$target = "images/";
$target = $target . basename( $_FILES['file_0']['name']);
$pic=($_FILES['file_0']['name']);
mysql_query("INSERT INTO images (listing_id, imgpath) VALUES ($lis, '$pic')");
if(move_uploaded_file($_FILES['file_0']['tmp_name'], $target)) {
	session_start();//temp test ok
	echo "clientID test = ". $_SESSION['client'];
	echo "The file ". basename( $_FILES['uploadedfile']). " has been uploaded, and your information has been added to the directory"; 
}
else {
//echo "Sorry, there was a problem uploading your file.";
}
}

 

 

Thanks

 

Rich

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.