Search code examples
phpprogressive-web-appsmanifestmanifest.json

in PWA, can file from share_target in manifest.json be fetched using PHP $_FILES using POST method?


the last time I tried (2020) I was able to fetch files uploaded using share_target method (yes, the web is already installed to the home screen using A2HS banner), I don't know what I did wrong, now when I try to fetch the file using $_FILES['upload']['tmp_name'], and check it using isset() and also if == NULL, It shows that the $_FILES is empty, but when I try using the form that I created to manually upload the file, the program runs normaly as it should be

here's some snippet:

1. manifest.json

    {
    "name": "SchedUIe",
    "short_name": "SchedUIe",
    "start_url": "home.php",
    "display": "standalone",
    "background_color": "#454545",
    "theme_color": "#454545",
    "orientation": "portrait-primary",
    "icons": [
        {
            "src": "images/logo144.png",
            "type": "image/png",
            "sizes": "144x144"
        },
        {
            "src": "images/logo192.png",
            "type": "image/png",
            "sizes": "192x192"
        },
        {
            "src": "images/logo512.png",
            "type": "image/png",
            "sizes": "512x512"
        }
    ],

    "share_target":
    {
        "action": "decision.php",
        "method": "POST",
        "enctype": "multipart/form-data",
        "params": 
        {
          "title": "name",
          "text": "description",
          "url": "link",
          "files": 
          [
            {
              "name": "upload",
              "accept": [".html", ".mhtml" ,".htm"]
            }
          ]
        }
    }
}

2. decision.php

<?php   
    $role = '';
    include_once('config_database.php');
    $uploaded = $_FILES['upload']['tmp_name'];
    $content = file_get_contents($uploaded);
    if($uploaded == NULL)
    {
        echo '<script language="javascript">';
        echo 'alert("Please select your file");';
        echo 'window.location="home.php";';
        echo '</script>';
        echo "<script>window.location.href='home.php';</script>";
    }
    elseif(strpos($content, 'title="Ganti Role">Mahasiswa') !== false) //some string checking logic
    {
        $role = 'mahasiswa';  
    }
    elseif(strpos($content, 'title="Ganti Role">Dosen') !== false) //some string checking logic
    {
        $role = 'dosen';
    }

    //Get the user name
    if ($role == 'mahasiswa')
    {
        include_once('output_mahasiswa.php');
        include_once('download_page.php');
    }
    elseif ($role == 'dosen')
    {
        include_once('output_dosen.php');
        include_once('download_page.php');
    }
    else
    {
        echo '<script language="javascript">';
        echo 'alert("Invalid File");';
        echo 'window.location="home.php";';
        echo '</script>';
        exit();
    }
?>

<html>
<head>
</head>
<body>

</body>
</html>

When I upload the file using share feature from android I always get the "please select your file" alert, which means it got the $uploaded == NULL condition. I already tried echo-ing by fetching using $_POST in php some test text using the "text" property also some url using the "url" property from the share_target and it worked, only the "files" that didn't work for me.

so is there any mistake in my program or it's just share_target doesn't support the $_FILES method anymore?


Solution

  • I have already found the solution which I don't really know precisely why this happen, so I uploaded the file using some 3rd party file manager, it shows that the file is not uploading, but when I try the file manager that is a native application from the phone it is successfully uploaded, I think its a permission thingy? I don't even know why I tried to share from that 3rd party file manager whilst there is already the native file manager one