PHP Multiple File Array

PHP Multiple File Array

you can use my updated code and as per my demo it is working perfect for multiple file upload

 <?php
if(isset($_FILES['documents'])){

foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
{
$file_name = $key.$_FILES['documents']['name'][$key];
$file_size =$_FILES['documents']['size'][$key];
$file_tmp =$_FILES['documents']['tmp_name'][$key];
$file_type=$_FILES['documents']['type'][$key];
move_uploaded_file($file_tmp,"galleries/".time().$file_name);
}
}else{
echo "<form enctype='multipart/form-data' action='test1.php' method='POST'>";
echo "File:<input name='documents[]' multiple='multiple' type='file'/><input type='submit' value='Upload'/>";

echo "</form>";
}
?>

Multiple files upload (Array) with CodeIgniter 2.0

I finally managed to make it work with your help!

Here's my code:

 function do_upload()
{
$this->load->library('upload');

$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];

$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
}
}

private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './Images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;

return $config;
}

Thank you guys!

Php ajax multiple file upload with new array

You are appending single file using this line of code myFormData.append('myFiles', file);. You have to use this code myFormData.append('myFiles[]', file); to send multiple file. Below is the code that i have implemented which can upload multiple files.

in your script place below code.

<script type="text/javascript">
$("#upload").change(function (event) {
var files = event.target.files;
var uploadingFile = [];
files = Object.values(files);
files.forEach(function (file) {
if (file.type.match('image')
|| file.type.match('application/vnd.ms-excel')
|| file.type.match('application/pdf')
|| file.type.match('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
|| file.type.match('application/vnd.openxmlformats-officedocument.presentationml.presentation')
) {
uploadingFile.push(file);
}
});

var myFormData = new FormData();
uploadingFile.forEach(function (file) {
myFormData.append('myFiles[]', file);
});

$.ajax({
url: 'upload.php',
type: 'POST',
processData: false, // important
contentType: false, // important
data: myFormData,success: function(data, textStatus, jqXHR)
{
console.log(data);
}
});
});
</script>

And in your upload php code place below code.

$target_path = "uploaded_files/";

$myFiles=$_FILES["myFiles"];
if(count($myFiles['name']) > 1){
$total = count($myFiles['name']);
for($i=0; $i<$total; $i++){
$ext = explode('.',$myFiles["name"][$i]);
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1];

if(move_uploaded_file($myFiles["tmp_name"][$i], $target_path)) {
echo "The file has been uploaded successfully \n";
} else{
echo "There was an error multiple uploading the file, please try again! \n";
}
}
} else {
$ext = explode('.',$myFiles["name"]);
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1];

if(move_uploaded_file($myFiles["tmp_name"], $target_path)) {
echo "The file has been uploaded successfully \n";
} else{
echo "There was an error uploading the file, please try again! \n";
}
}

PHP get names of multiple uploaded files

Thanks for the help, i got it to work by simply creating variables:

$image_1 = (print_r($files['name'][0], true)); 
$image_2 = (print_r($files['name'][1], true));
$image_3 = (print_r($files['name'][2], true));

Multiple files uploading in multidimensional array

There were some changes to be made, in addition to the ones in the comments. Major issues are:

  1. You were trying to get both files and post data into one single array, when both $_POST and $_FILES are, in PHP nature, totally separated entities. So you're ending up trying to access a single array row when there is actually two row arrays inside both these superglobals.
  2. Your $target_file was never declared, and your $target_dir has too many slashes.
  3. The item 1 made you access $val the wrong way.

These are the final corrections I came up with, keeping the logic for your own environment. The explanation to each part is commented inside the code.

HTML form

<!DOCTYPE html><html><head></head><body> <form action="upload.php" method="post" enctype="multipart/form-data">            <table> <?php $counter = 2; // number of fields you want to be printed for($i = 0; $i < $counter; $i++): // do proper counting ?>                <tr>   <td>    <input type="hidden" name="row[]" value="<?php echo $i; ?>"/> File:   </td>   <td>    <input type="file" name="row[]" >   </td>  </tr> <?php endfor; ?>     </table>     <input type="submit" value="Upload" name="submit"> </form></body></html>

Uploading multiple files userfile[] array?

I had this same issue on a project last year, after a bit of searching I found a perfect function.

I take no credit it for it, but cannot remmeber where I found it so if anyone knows please link back to the author.

Make sure the form has the files named like this

<input type="file" name="userfile"/>
<input type="file" name="userfile2" />
<input type="file" name="userfile3" /> ..etc

Or

 <input type="file" name="userfile[]" />

The function..

function multiple_upload($upload_dir = 'uploads', $config = array())
{
$files = array();

if(empty($config))
{
$config['upload_path'] = '../path/to/file';
$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
$config['max_size'] = '800000000';
}

$this->load->library('upload', $config);

$errors = FALSE;

foreach($_FILES as $key => $value)
{
if( ! empty($value['name']))
{
if( ! $this->upload->do_upload($key))
{
$data['upload_message'] = $this->upload->display_errors(ERR_OPEN, ERR_CLOSE); // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file
$this->load->vars($data);

$errors = TRUE;
}
else
{
// Build a file array from all uploaded files
$files[] = $this->upload->data();
}
}
}

// There was errors, we have to delete the uploaded files
if($errors)
{
foreach($files as $key => $file)
{
@unlink($file['full_path']);
}
}
elseif(empty($files) AND empty($data['upload_message']))
{
$this->lang->load('upload');
$data['upload_message'] = ERR_OPEN.$this->lang->line('upload_no_file_selected').ERR_CLOSE;
$this->load->vars($data);
}
else
{
return $files;
}
}

Now it's been a while since I've used it so if you need any extra help setting it up just let me know.

Upload multiple files PHP

  $count = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
for ($i=0;$i<count($_FILES['file']);$i++) {
if (strlen($_FILES['file']['name'][$i]) > 1) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], 'upload/'.$name)) {
$count++;
}
}
}
}


Related Topics



Leave a reply



Submit