douyuan9512 2016-08-05 04:38
浏览 24
已采纳

在php中处理文件上传的大量错误

I'm using this code to process uploaded files:

mkdir("files/" . $id, 0700);
$path = "files/" . $id;
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
        foreach($_FILES['attachments']['name'] as $f => $name)
        {
            if($_FILES['attachments']['error'][$f] == 4)
            {
                continue;
            }
            if($_FILES['attachments']['error'][$f] == 0)
            {
                if(move_uploaded_file($_FILES["attachments"]["tmp_name"][$f], $path.$name))
                {
                    $count++;
                }
            }
        }
    }

$id is a random number taken from the database. Besides, I'm using this markup:

<input type="file" id="attachments" name="attachments[]" multiple="multiple" accept="*">

While the exact same code had worked brilliantly before, it now throws numbers of errors I can't really deduce:

1: mkdir(): File exists in ... on line ... (<-- now, it doesn't for granted!)
2: Undefined index: attachments in ... on line ... (well, it's defined also using form method post!)
3: Invalid argument supplied for foreach() in ... on line ... (which is quite clear as the above stated errors do prevent the foreach from doing its job correctly)

Yes, I made sure that I'm actually using POST. I also tried changing the file input's name from attachments to any other, however, scenario remains the same.

Adding enctype="multipart/form-data" has done it.

  • 写回答

2条回答 默认 最新

  • duanhuoyao7011 2016-08-05 04:47
    关注

    1] Check for the folder rights 0777. Weather you are able to create directory or not

    2] After posting form. Make sure your form has enctype = multipart/form-data tag. In you file please check with

    echo "<pre>";
    print_r($_FILES);
    exit;
    

    If you are getting any data or not? If getting data then move ahead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度