dongmei5168 2013-06-06 23:28
浏览 16
已采纳

php文件上传未定义的偏移通知[重复]

This question already has an answer here:

I have a bit of cod the works fine on MAMP but gives me an error on WAMP. The code is:

$uploads = ""; 
for($x=0; $x<$count_data; $x++)
{
    $uploads .= $_FILES['file']['name'][$x] . ' ';
}

And the error is:

Notice: Undefined offset: 1

Notice: Undefined offset: 2

Notice: Undefined offset: 3

Notice: Undefined offset: 4

Any ideas?

Here is the file up load code:

$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/../uploads/'; //set upload directory
$count_data=count($_FILES['file']) ;
$uploads = ""; 
for($x=0; $x<$count_data; $x++)
{
    $uploads .= $_FILES['file']['name'][$x] . ' ';
}



for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
    $number_of_file_fields++;
    if ($_FILES['file']['name'][$i] != '') { //check if file field empty or not
        $number_of_uploaded_files++;
        $uploaded_files[] = $_FILES['file']['name'][$i];
        if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . $orderNumber . "_" . $_FILES['file']['name'][$i])) {
            $number_of_moved_files++;
        }
    }
}
</div>
  • 写回答

1条回答 默认 最新

  • drfu80954 2013-06-06 23:41
    关注

    There's 2 ways for multiple file uploads to be organized in the $_FILES array, depending on whether they have the same name attribute or not.

    To handle both cases, try something like this:

    $uploads = '';
    foreach ($_FILES as $inputName => $fileInfo) {
        foreach ((array) $fileInfo['name'] as $n) {
            $uploads .= $n . ' ';
        }
    }
    

    To help debug the different between the two hosts, try echo'ing out the contents of the $_FILES array on each and see how it's different.

    print_r($_FILES);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答