Jump to content


Photo

Android file uploads


  • Please log in to reply
6 replies to this topic

#1 2inept

2inept
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 14 May 2018 - 11:15 AM

Hi

I have some PHP to upload files in a webbrowser but it seems to do an odd thing.

 

This problem could be with the php code or it could be an Android thing, I'm not sure.

 

It is supposed to send a multi file array.

 

On an Android, if you select

 

Documents/Images/Camera then multi select - it works fine

 

If you select

 

Documents/Photos/Camera and multi select - the phone only sends 1 image

 

So there seems to be a difference between "Images" and "Photos" but I don't understand why.

 

Any ideas?

 

Thanks

<?php

date_default_timezone_set('Europe/London');

$logfile = "C:\\Windows\\temp\\files.txt";

$extensions = array('jpeg', 'jpg', 'png', 'gif');
$dir = 'uploads/';
$count = 0;

if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['files']))
{
  $files = multiple($_FILES);

  //file_put_contents ( $logfile , print_r($files,TRUE), FILE_APPEND | LOCK_EX);

  $fileCount = count($files['files']);

  for ($i = 0; $i < $fileCount; $i++)
  {
	// if file not uploaded then skip it
	if ( is_uploaded_file($files['files'][$i]['tmp_name']) )
	{

		$imageFileType = pathinfo($files['files'][$i]['name'],PATHINFO_EXTENSION);

		// skip suppoted files
		//if( !in_array($imageFileType, $extensions) )
		//  continue;

		$filename = stripslashes($files['files'][$i]['name']);
		$extension = strtolower($imageFileType);
		$file_name=gmdate("Ymd_His",time())."_".rand(0,100000);
		$newname=$dir.$file_name.'.'.$extension;

		  if( copy($files["files"][$i]["tmp_name"], $newname) )
			$count++;
	}
  }

  echo json_encode(array('count' => $count));
  header('Location: /index.php');
  exit;
}


function multiple(array $_files, $top = TRUE)
{
    $files = array();
    foreach($_files as $name=>$file){
        if($top) $sub_name = $file['name'];
        else    $sub_name = $name;

        if(is_array($sub_name)){
            foreach(array_keys($sub_name) as $key){
                $files[$name][$key] = array(
                    'name'     => $file['name'][$key],
                    'type'     => $file['type'][$key],
                    'tmp_name' => $file['tmp_name'][$key],
                    'error'    => $file['error'][$key],
                    'size'     => $file['size'][$key],
                );
                $files[$name] = multiple($files[$name], FALSE);
            }
        }else{
            $files[$name] = $file;
        }
    }
    return $files;
}

?>

Edited by 2inept, 14 May 2018 - 11:16 AM.


#2 mac_gyver

mac_gyver
  • Staff Alumni
  • Staff Alumni
  • 4,148 posts

Posted 14 May 2018 - 11:48 AM

what is the size of the files that work and what are the sizes of the files that don't work?


Edited by mac_gyver, 14 May 2018 - 11:48 AM.

multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting.

#3 2inept

2inept
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 14 May 2018 - 01:06 PM

They are all the same files in the same camera roll. I've tried with small screen shots to native Samsung J5 camera full res.



#4 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,194 posts
  • LocationVoorheesville NY

Posted 14 May 2018 - 01:19 PM

What does

"If you select
Documents/Photos/Camera"

refer to?
JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#5 2inept

2inept
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 14 May 2018 - 01:37 PM

When you hit a button to start the upload, the Android device opens up it's file browser, there are the 2 methods I described above, and they seem to do different things.



#6 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,194 posts
  • LocationVoorheesville NY

Posted 14 May 2018 - 10:18 PM

If that is the answer to my question I am in the wrong forum. Not familiar with Anroids bu1t if you are talking about features supplied by the phone rather than the application, why are you asking us here for help?
JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#7 2inept

2inept
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 15 May 2018 - 07:36 AM

Because I don't know if it's the Android or the PHP that is the problem, I thought that maybe someone would have come across the same issue and be able to say definitvly it's Android and nothing you can do about it or, change the PHP and do XYZ instead.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users