Jump to content

Copy file loop


petenaylor

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ");
}
}

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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.