2

I need to upload multiple files per request from my Android application to an apache webserver. I am on a Windows 10 computer and used XAMPP to set up the webserver.

When I upload a single file, everything works as intended. But when posting multiple fails they appear in the C:\xampp\tmp Folder, but then don't get moved.

This is how the $_FILES array looks like

<?php $_FILES = array (

 'uploaded_file1' => 
  array (
    'name' => 'data1.csv',
    'type' => 'text/csv',
    'tmp_name' => 'C:\\xampp\\tmp\\phpA9A6.tmp',
    'error' => 0,
    'size' => 7991,
  ),
  'uploaded_file2' => 
  array (
    'name' => 'data2.csv',
    'type' => 'text/csv',
    'tmp_name' => 'C:\\xampp\\tmp\\phpA9A7.tmp',
    'error' => 0,
    'size' => 24906727,
  ),
  'uploaded_file3' => 
  array (
    'name' => 'data3.csv',
    'type' => 'text/csv',
    'tmp_name' => 'C:\\xampp\\tmp\\phpB244.tmp',
    'error' => 0,
    'size' => 12342379,
  ),
);

And this is the script I am trying to move them with

<?php

$move = "images/";
foreach ($_FILES['uploaded_file']['tmp_name'] as $key => $value) 
{
     $tmp_name = $_FILES['uploaded_file']['tmp_name'][$key];
     $name = $move.basename($_FILES['uploaded_file']['name'][$key]);
     move_uploaded_file($tmp_name, $name);
}

?>

I think it has to do with the name of the uploaded files, but when I try to set the name of each file to uploaded_file, the $_FILES array looks like this

<?php $_FILES = array (
  'uploaded_file' => 
  array (
    'name' => 'data3.csv',
    'type' => 'text/csv',
    'tmp_name' => 'C:\\xampp\\tmp\\phpEF74.tmp',
    'error' => 0,
    'size' => 12342379,
  ),
);

I am not really experienced with HTTP and PHP so this is probably a trivial issue, but even with several different versions of the PHP script I cannot figure this out. Any help is greatly appreciated.

EDIT: The target directory C:\xampp\htdocs\images exists and I have adjusted the maximum upload and POST file size, but as I said only the moving the files is the issue.

1 Answers1

0

I have found the solution to my problem, so I'll post it here in case anyone has a similar issue. The problem was in how the $_FILES array is structured. From the php manual, this is how the $_FILES array usually looks when sending a $POST request.

Array
(
    [name] => Array
        (
            [0] => facepalm.jpg
            [1] => 
        )

    [type] => Array
        (
            [0] => image/jpeg
            [1] => 
        )

    [tmp_name] => Array
        (
            [0] => /tmp/phpn3FmFr
            [1] => 
        )

    [error] => Array
        (
            [0] => 0
            [1] => 4
        )

    [size] => Array
        (
            [0] => 15476
            [1] => 0
        )
)

When building the multipartbody with the okhttp library, my $_FILES array looked like what I posted before.

Here is the adjusted script to handle the upload:

<?php

$path = "images/";

foreach($_FILES as $file){
    $name=$file['name'];

    $file_path = $path.basename($name);

    if(move_uploaded_file($file['tmp_name'],$file_path))    {
        echo "success";
    }else {
        echo "error";
    }   
}
?>