html - Uploading multiple file to server: getting PHP code wrong -


i have following html , php codes handle uploading of multiple files folder in server. 1 file (from selected upload files) gets uploaded server. please advise me on doing wrong? starting php/html , clueless task.

html:

<form action="upload_multi15_action.php" method="post" enctype="multipart/form-data">      select files upload: <br />          <input name="userfile[]" type="file" /><br />          <input name="userfile[]" type="file" /><br />           <input name="userfile[]" type="file" /><br />          <input name="userfile[]" type="file" /><br />           <input name="userfile[]" type="file" /><br />          <input name="userfile[]" type="file" /><br />          <br />          <input type="submit" value="upload files">      </form>

php:

    $count=0;        echo '<pre>';      foreach($_files['userfile']['name'] $to_upload){              $upload = "./".basename($to_upload);          move_uploaded_file($_files['userfile']['tmp_name'][$count], $upload) or                     die("possible file upload attack!\n");          $count = $count++;          $upload = '';      }      print "</pre>";

i totally lost on doing wrong php code.

$count = $count++; 

as it's written here, expect 0. post-increment operator returns value , increments it. assign returned value same variable. next time tries move uploaded file, no longer exists because you've moved first file.

you don't need keep counter way anyway, can foreach give index, i've done here.

<?php echo '<pre>'; foreach($_files['userfile']['name'] $count => $to_upload){      if($_files['userfile']['error'][$count] != 0) {         echo "error {$_files['userfile']['error'][$count]} on $count<br>";         continue;     }     $upload = "./".basename($to_upload);     move_uploaded_file($_files['userfile']['tmp_name'][$count], $upload) or                die("possible file upload attack!\n");     $upload = ''; } print "</pre>"; 

as side note, should checking incoming names. example: it's written can replace upload file own uploading php file same name. [edit] it's idea check incoming data if you're expecting files specific kind of file. making sure file can parsed getimagesize if you're expecting image files, instance.


alternatively, if didn't want use index foreach , wanted keep increment-based method, change $count = $count++; $count++; removes assignment of post-increment return value $count variable, causing $count never change.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -