Uploading multiple image on php server from android

后端 未结 3 392
一向
一向 2020-12-09 00:24

Help for this specific server side php code I don\'t have any knowledge of php and I have to upload three images from android to this php page.

I have tried many met

3条回答
  •  攒了一身酷
    2020-12-09 01:03

    First of all your code could be optimized to the following:

    $files = array('file1', 'file2', 'file3');
    $path = 'elp/pendingimages/';
    
    foreach ($files as $file) {
        if ($_FILES[$file]['error'] > 0) {
            echo 'Error: '. $_FILES[$file]['error'] .'
    '; } else { echo 'Upload: '. $_FILES[$file]['name'] .'
    '; echo 'Type: '. $_FILES[$file]['type'] .'
    '; echo 'Size: '. ($_FILES[$file]['size'] / 1024) .' Kb
    '; echo 'Stored in: '. $_FILES[$file]['tmp_name'] .'
    '; } $basename = basename($_FILES[$file]['name']); if (move_uploaded_file($_FILES[$file]['tmp_name'], $path . $basename) { echo "The file {$basename} has been uploaded"; } else { echo 'There was an error uploading the file, please try again!'; } }

    If you're using different field for each file then it's fine.

    Next you can see what $_FILES array store in itself when it's multiple upload:

    $_FILES = array(
        ['files'] => array(
            ['name'] => array(
                [0] => 'WALL_video.jpg'
                [1] => 'WALLc.jpg'
            )
            ['type'] => array(
                [0] => 'image/jpeg'
                [1] => 'image/jpeg'
            )
            ['tmp_name'] => array(
                [0] => '/tmp/phpnbKcdM'
                [1] => '/tmp/phpnrHSN1'
            )
            ['error'] => array(
                [0] => 0
                [1] => 0
            )
            ['size'] => array(
                [0] => 885968
                [1] => 839713
            )
        )
    )
    

    The following code will work for you if you're using one field with name like files[] as array of files.

    $target_path = 'elp/pendingimages/';
    
    foreach ($_FILES['files']['name'] as $index => $name) {
        if ($_FILES['files']['error'][$index] > 0) {
            echo 'Error: ' . $_FILES['files']['error'][$index] . '
    '; } else { echo 'Upload: '. $_FILES['files']['name'][$index] .'
    '; echo 'Type: '. $_FILES['files']['type'][$index] .'
    '; echo 'Size: '. ($_FILES['files']['size'][$index] / 1024) .' Kb
    '; echo 'Stored in: '. $_FILES['files']['tmp_name'][$index] .'
    '; } $path = $target_path . basename($name); if (move_uploaded_file($_FILES['files']['tmp_name'][$index], $path) { echo "The file {$name} has been uploaded"; } else { echo 'There was an error uploading the file, please try again!'; } }

提交回复
热议问题