petenaylor Posted January 8, 2012 Share Posted January 8, 2012 Hi all I am trying to write a piece of code that copies files from a multiple selected form. Here's the form: <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="image[]" multiple="multiple" /> <input name="submit" type="submit" value="submit"> </form> Here's my PHP code: if(isset($_POST['submit'])) { $count=0; foreach($_FILES['image'] as $filename){ copy($_FILES['image']['tmp_name'], WEB_UPLOAD."/images/galleries/".$filename) or die("Error uploading image "); $count++; } } I get the message, Array to string conversion and no such directory found. Am I right in thinking I am sending the files as arrays? Many thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/254605-copy-file-loop/ Share on other sites More sharing options...
PaulRyan Posted January 8, 2012 Share Posted January 8, 2012 Try changing this: copy($_FILES['image']['tmp_name'], WEB_UPLOAD."/images/galleries/".$filename) or die("Error uploading image "); To this: copy($filename['tmp_name'], WEB_UPLOAD."/images/galleries/".$filename['name']) or die("Error uploading image "); You're assign the variable $filename to the array of data for each separate image, use $filename as the array, and then you can call each element from it. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/254605-copy-file-loop/#findComment-1305574 Share on other sites More sharing options...
petenaylor Posted January 8, 2012 Author Share Posted January 8, 2012 Hi there Thanks for your quick reply, I have changed that bit of code but now I get the following message: Warning: copy(I) [function.copy]: failed to open stream: No such file or directory in Cheers Pete Quote Link to comment https://forums.phpfreaks.com/topic/254605-copy-file-loop/#findComment-1305576 Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2012 Share Posted January 8, 2012 Out of curiosity, is there a reason you're using copy() rather than move_uploaded_file()? Quote Link to comment https://forums.phpfreaks.com/topic/254605-copy-file-loop/#findComment-1305585 Share on other sites More sharing options...
petenaylor Posted January 8, 2012 Author Share Posted January 8, 2012 Hi there Just tried that, I still get 'Error Uploading file' I have changed the code slightly: if(isset($_POST['submit'])) { foreach($_FILES['image']['name'] as $filename){ move_uploaded_file($filename['tmp_name'], WEB_UPLOAD."/images/galleries/".$filename['name']) or die("Error uploading image "); } } Quote Link to comment https://forums.phpfreaks.com/topic/254605-copy-file-loop/#findComment-1305589 Share on other sites More sharing options...
PaulRyan Posted January 8, 2012 Share Posted January 8, 2012 Try using this instead and tell me what it outputs? <?PHP if(isset($_POST['submit'])) { $count=0; foreach($_FILES['image'] as $filename){ echo '<pre>'; print_r($filename); echo '</pre>'; //move_uploaded_file($_FILES['image']['tmp_name'], WEB_UPLOAD."/images/galleries/".$filename) or die("Error uploading image "); $count++; } } //### exit as we want just the plain text output from above exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254605-copy-file-loop/#findComment-1305627 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.