WebClient UploadFile в PHP $ _FILES? - PullRequest
0 голосов
/ 21 июня 2019

Я загружаю файл в PHP на хосте CentOS с помощью C # WebClient () следующим образом:

            using (WebClient wc = new WebClient())
            {
                NameValueCollection parameters = new NameValueCollection();
                parameters.Add("computer", _mach_name);
                parameters.Add("ipadr", _localIP);
                wc.QueryString = parameters;

                byte[] bret = wc.UploadFile(url, "POST", schFile);
                string sret = Encoding.ASCII.GetString(bret);
            }

У меня есть два отдельных компьютера, использующих один и тот же код выше, поэтому они оба отправляют один и тот же файл schFile в PHP $ _FILES, но я могу определить, какой компьютер по _mach_name и _localIP.

Мой PHP-код для получения этого файла выглядит следующим образом:

$ds = DIRECTORY_SEPARATOR;
$storeFolder = 'schedule';
date_default_timezone_set('America/Los_Angeles');
$time = strtotime(" ");
$time = date("M,d,Y h:i:s A");

$test = print_r($_FILES, true);

if (!empty($_FILES)) {
if(is_uploaded_file($_FILES['file']['tmp_name'])) {
    $name = $_GET['computer'];  //get the computer name so I know if this is the north or south side schedule
    $ipadr = $_GET['ipadr'];    //get the computer ip address - another way to tell whos schedule this is
    file_put_contents("upload_schedule_debug.txt", "time: $time   name: $name   ip: $ipadr    _FILES:  $test\r\n", FILE_APPEND);

    $error = $_FILES['file']['error'];
    if($error == UPLOAD_ERR_OK) {
        $tempFile = $_FILES['file']['tmp_name'];
        $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;

        //Add computer name to front of schedule filename - ie SV-North-PC_player_schedule.db3 / SV-South-PC_player_schedule.db3
        $filename = $name . "_" . $_FILES['file']['name'];
        $targetFile =  $targetPath . $filename;
        move_uploaded_file($tempFile, $targetFile);
        //$_FILES = array();  //clear the $_FILES
    }
}

}

Этот код выше работал в течение нескольких месяцев просто отлично - потом недавно начал собирать старый ранее загруженный файл, а не последний загруженный файл? Так что же происходит, если он переключается между старым устаревшим загруженным файлом и новым правильным только что загруженным файлом? Я не знаю, почему это делает кто-нибудь может помочь? Вот моя отладка из file_put_contents (…)

time: Jun,21,2019 09:40:00 AM   name: SV-North-PC   ip:
192.168.203.102    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phpYUCDQZ
            [error] => 0
            [size] => 9216
        )

)

time: Jun,21,2019 09:40:06 AM   name: SV-South-PC   ip:
192.168.203.103    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phpG25EYe
            [error] => 0
            [size] => 7168
        )

)

time: Jun,21,2019 09:40:07 AM   name: SV-South-PC   ip:
192.168.203.103    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phpfTPhdC
            [error] => 0
            [size] => 7168
        )

)

time: Jun,21,2019 09:40:13 AM   name: SV-South-PC   ip:
192.168.203.103    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phppqHvD6
            [error] => 0
            [size] => 10240
        )

)

time: Jun,21,2019 09:40:13 AM   name: SV-South-PC   ip:
192.168.203.103    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phpqhOHFH
            [error] => 0
            [size] => 10240
        )

)

time: Jun,21,2019 09:40:21 AM   name: SV-North-PC   ip:
192.168.203.102    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phpN0DdNK
            [error] => 0
            [size] => 7168
        )

)

time: Jun,21,2019 09:40:21 AM   name: SV-North-PC   ip:
192.168.203.102    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phpVIiSuD
            [error] => 0
            [size] => 7168
        )

)

time: Jun,21,2019 09:40:30 AM   name: SV-North-PC   ip:
192.168.203.102    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phphruWhV
            [error] => 0
            [size] => 9216
        )

)

time: Jun,21,2019 09:40:37 AM   name: SV-South-PC   ip:
192.168.203.103    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/php6yH4Dj
            [error] => 0
            [size] => 7168
        )

)

time: Jun,21,2019 09:40:37 AM   name: SV-South-PC   ip:
192.168.203.103    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phpu2KuxL
            [error] => 0
            [size] => 7168
        )

)

time: Jun,21,2019 09:40:44 AM   name: SV-South-PC   ip:
192.168.203.103    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phphAbaWx
            [error] => 0
            [size] => 10240
        )

)

time: Jun,21,2019 09:40:44 AM   name: SV-South-PC   ip:
192.168.203.103    _FILES:  Array (
    [file] => Array
        (
            [name] => player_schedule.db3
            [type] => application/octet-stream
            [tmp_name] => /var/www/www-root/data/mod-tmp/phpR0RGMd
            [error] => 0
            [size] => 10240
        )

)
...