12

I created a very basic example of a multiple file upload form (reference), it works perfect on desktop but not on mobile, at least the ones I am testing with.

On Mobile (Xiaomi Mi4 [Android version: 6.1] - Google Chrome/Mozilla Firefox): When I click on Choose files I see this screen:
enter image description hereenter image description here

If I choose Google Photos and select multiple files, only the first file will be inserted into the form. If I select the Gallery (native) app and select multiple files I get the correct number on the form but when I click upload I get the "Aw Snap" screen:

enter image description here

Any idea why this is happening?

Besides Google Photos and the native app I tried 5 different apps, the last one, Piktures actually worked!

Please tell me this is not an app thing... is there a way to get the files correctly?

Code attached:

<form method="post" enctype="multipart/form-data">
        <input type="file" name="my_file[]" multiple>
        <input type="submit" value="Upload">
    </form>
    <?php
        if (isset($_FILES['my_file'])) {
            $myFile = $_FILES['my_file'];
            $fileCount = count($myFile["name"]);

            for ($i = 0; $i < $fileCount; $i++) {
                ?>
                    <p>File #<?= $i+1 ?>:</p>
                    <p>
                        Name: <?= $myFile["name"][$i] ?><br>
                        Temporary file: <?= $myFile["tmp_name"][$i] ?><br>
                        Type: <?= $myFile["type"][$i] ?><br>
                        Size: <?= $myFile["size"][$i] ?><br>
                        Error: <?= $myFile["error"][$i] ?><br>
                    </p>
                <?php
            }
        }
    ?>

If you wish to test: http://odedta.com/projects/jqueryfileupload/

Thanks!

odedta
  • 2,430
  • 4
  • 24
  • 51
  • As result state that, browser is now out of memory it can have multiple scenarios which can cause this issue, 1. Files you are trying to upload are too large, 2. Are you manipulating files using javascript api ? – Touqeer Shafi Nov 01 '17 at 14:23
  • @TouqeerShafi Hey, 1. The files are about 80KB each and i'm trying 2-3 at a time. 2. In the app that i'm making i'm using DropzoneJS, since I couldn't figure out what's wrong I created this simplest example to eliminate external class issues or a JS issue, this is pure PHP/HTML and this bug is still happening... I was hoping that maybe there is a JS solution out there is compensates for this bug... similar to JS classes of datepickers that increase functionality of the original `input[type="date"]` – odedta Nov 01 '17 at 15:00
  • What version android and Google Chrome? Did u check other mobile browser or native? – Fortran Nov 02 '17 at 14:13
  • What file u choose?(what format ? Try use two png/jpeg/txt/mp3/binary files files). As sound bug in your version Chrome(not correct retrieve and handle "Intent.ACTION_SEND_MULTIPLE"). I read. Next version Android it fixed! – Fortran Nov 02 '17 at 14:22
  • I'm curious if this [TryIt link](https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_multiple) works for you. – Cheticamp Nov 02 '17 at 14:23
  • @Fortran I am using Chrome (Android) version: 61.0.3163.98 (latest) for this device. When I try the native browser, it doesn't allow multiple file uploads at all, it's 1 file at a time. I upload 3 images, 80KB each, jpg format. It does seem like an Android bug, not a Chrome bug as I have tried Firefox as well and saw the same issue. – odedta Nov 05 '17 at 10:47
  • @Cheticamp same error, this seems like an Android bug. – odedta Nov 05 '17 at 10:49
  • 1
    @odedta I agree: An Android bug or just a poorly reported limitation. It seems that multiple file uploads has been an issue in the past. For what it's worth, take a look at [this](https://stackoverflow.com/questions/25412406/upload-multiple-files-on-android-using-input-type-file-multiple) especially the "caniuse" link. If you haven't already, try uninstalling all apps related to the problem, rebooting the device, and then reinstall the apps in case things are in some odd state and need to be reinitialized. – Cheticamp Nov 05 '17 at 14:28
  • @Cheticamp The easier way is to try with another Xiaomi device I guess hehe. `caniuse` website actually says that it is supported, oh well... Thanks for the input! – odedta Nov 05 '17 at 17:47
  • It works without a problem on Tommy Wiko with Chrome 61.0.3163.98 and Android 6.0.1 – svujic Nov 07 '17 at 18:26
  • Thanks for the input @svujic – odedta Nov 08 '17 at 10:49

2 Answers2

8

Try this might help you this is only front end part of file upload with js

window.onload = function() {
  if (window.File && window.FileList && window.FileReader) {
    var filesInput = document.getElementById("uploadImage");
    filesInput.addEventListener("change", function(event) {
      var files = event.target.files;
      var output = document.getElementById("result");
      for (var i = 0; i < files.length; i++) {
        var file = files[i];
        if (!file.type.match('image'))
          continue;
        var picReader = new FileReader();
        picReader.addEventListener("load", function(event) {
          var picFile = event.target;
          var div = document.createElement("div");
          div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
            "title='" + picFile.name + "'/>";
          output.insertBefore(div, null);
        });        
        picReader.readAsDataURL(file);
      }

    });
  }
}
<input type="file" id="uploadImage" name="termek_file" class="file_input" multiple/>
<div id="result" class="uploadPreview">
Nisal Edu
  • 7,237
  • 4
  • 28
  • 34
2

I am not sure exactly about that mobile browsers are support multiple attribute I read some article it is not support or is not standart, any way If you want to post multiple image; You can see full code below

First Step;

You should use FileReader for load on browser as locally

Multiple file loading with FileReader

document.getElementById("filesToUpload").onchange = function () {
    var fileIndex = 0;
    for ( var x= 0; x < input.files.length; x++) {
        //add to list
        var li = document.createElement('li');
        //Filename listing
        li.setAttribute('id','li_'+x);
        li.innerHTML = 'File ' + (x + 1) + ':  ' + input.files[x].name;
        list.append(li);
        //FileReader create and set onload event
        var reader = new FileReader();
        reader.onload = function (data) {
            data.target.result;
        }
        //Read file
        reader.readAsDataURL(input.files[x]);
    }
}

Second Step

You can set image content to textarea as base64 data for each file

reader.onload = function (data) {
    //....
    //Split base64 data from DataURL (// "data:image/png;base64,.....)
    var b64 = data.target.result.split(',')[1];
    var b64Input = document.createElement('textarea');
    b64Input.setAttribute('name','file64[]');
    b64Input.value = b64;
    //...
}

Third Step

Then you can send to your php file whole data and save your content as image

for($i = 0; $i<count($_POST["fileName"]);$i++){
    echo $_POST["fileName"][$i]."<br>";
    //decode base64 content and put file
    file_put_contents($_POST["fileName"][$i], base64_decode($_POST["file64"][$i]));
}

Full Code

HTML & JavaScript

<input name="filesToUpload[]" id="filesToUpload" type="file" multiple />
<form method="post" action="multipleFileUpload.php" enctype="multipart/form-data" id="formMultipleFileUpload">
<ul id="fileList">

</ul>
<input type="submit" value="Upload" id="btnUpload">
</form>
<script type="text/javascript">
var input = document.getElementById('filesToUpload');
var list = document.getElementById('fileList');
document.getElementById("filesToUpload").onchange = function () {
    var fileIndex = 0;
    for ( var x= 0; x < input.files.length; x++) {
        //add to list
        var li = document.createElement('li');
        li.setAttribute('id','li_'+x);
        li.innerHTML = 'File ' + (x + 1) + ':  ' + input.files[x].name;
        list.append(li);
        var reader = new FileReader();
        reader.onload = function (data) {
            var li = document.getElementById('li_'+fileIndex);
            var previewImg = document.createElement('img');
            previewImg.setAttribute('width','50');
            previewImg.setAttribute('height','50');
            li.append(previewImg);
            previewImg.src = data.target.result;
            var b64 = data.target.result.split(',')[1];
            var b64Input = document.createElement('textarea');
            b64Input.setAttribute('name','file64[]');
            b64Input.value = b64;
            li.append(b64Input);
            var fileName = document.createElement('input');
            fileName.setAttribute('name','fileName[]');
            fileName.value = input.files[fileIndex].name;
            li.append(fileName);
            fileIndex++;
        }
        reader.readAsDataURL(input.files[x]);
    }
}

PHP (multipleFileUpload.php)

<?php
for($i = 0; $i<count($_POST["fileName"]);$i++){
    echo $_POST["fileName"][$i]."<br>";
    file_put_contents($_POST["fileName"][$i], base64_decode($_POST["file64"][$i]));
}
?>

Working screenshot Preview Screen Uploaded Screen

Ferhat BAŞ
  • 797
  • 7
  • 12
  • I put your solution up here: http://odedta.com/projects/jqueryfileupload/ferhat/ - I still get the same error although, there is an improvement, now, I am able to select 3 files and the browser recognizes 3 files which never happend with most of the plugins out there. – odedta Nov 08 '17 at 22:06
  • Hi @odedta I think there is a problem on your server because it should look like this `https://jsfiddle.net/87c1put3/` also you can see my uploaded file. – Ferhat BAŞ Nov 08 '17 at 22:22
  • @odedta I have added screenshot on my phone tested via jsfidle sample and files uploded to be yor server. – Ferhat BAŞ Nov 08 '17 at 22:30
  • https://imgur.com/a/qEi5G - I guess some devices support it and some don't. Which app did you use to upload the files? – odedta Nov 09 '17 at 09:42
  • 1
    What is your images size? if it is not big then means it is depend with device, also my device is note 5, chrome and I have tested another device which is LG G3 with chrome but it took huge time for drawing pictureand completing upload – Ferhat BAŞ Nov 09 '17 at 10:41
  • There you go, then it's a device specific problem and from what you describe it might have to do with memory. I upload 80KB images to avoid any problems of this sort. – odedta Nov 09 '17 at 10:55
  • Thanks for your time and help, much appreciated! :-) – odedta Nov 09 '17 at 11:26