douxu5845 2014-12-09 21:08
浏览 45
已采纳

PHP:多个文件上传随机工作

I have a problem with my multiple file uploading script.

Here's my code :

Admin.php:

<form method="post" action="upload.php" enctype="multipart/form-data">
     <input multiple="multiple" type="file" name="file[]">
     <input type="submit" name="send" value="Envoyer Images">
</form>

Upload.php:

<?php
session_start();

$message = '';

$extensions = array('.png', '.jpg', '.jpeg', '.JPG', '.PNG', '.JPEG');

try{
    $db = new PDO('mysql:host=localhost;dbname=db', 'root', '');
}
catch(Exception $e){
    die('Erreur : ' . $e->getMessage());
}

$query = $db->prepare('SELECT max(id) as id FROM t_picture');
$query->execute();
$data = $query->fetch();

$i = $data['id'] == NULL ? 0 : $data['id'];

echo 'i = ' . $i;

if(isset($_FILES['file'])){
    $dossier = 'pictures/';

    $message = '#1 Reached';

    foreach ($_FILES['file']['tmp_name'] as $index => $tmpName){

        $message = '#2 Reached';

        if(!empty($_FILES['file']['error'][$index])){
            $message = 'There is an error #2';
            break;
        }

        $ext = strrchr($_FILES['file']['name'][$index], '.');

        $tmp_name = strval($i) . $ext;

        echo 'Name : ' . $tmp_name;


        if(!empty($tmpName) && is_uploaded_file($tmpName) && in_array(strrchr($tmp_name, '.'), $extensions)){
            echo '#4 Reached';
            move_uploaded_file($tmpName, $dossier . $tmp_name);
            $i++;
            $message += '<p>Index '. strval($i) .' uploaded !</p>';
        }

    }
}
else{
    $message ='Is not set';
}

echo $message;

The thing is, I can upload one or multiple files, it works according to the files i'm uploading.

When the problem occured, i think that's because i'm bursting a 2M limit but i've changed the max_file_size value in my php.ini, and my phpinfo() correctly indicate 64000M (Test value).

So when it desn't work, i have an Undifined Index for each call to $_FILES['file'].. Do you know what's wrong?

  • 写回答

1条回答 默认 最新

  • doumeng06063991 2014-12-09 21:28
    关注

    Have you checked? in your php.ini

    upload_max_filesize     "64M" //thats OK
    post_max_size   "??M"   //??  
    max_file_uploads    ??  //how many files? 
    

    post max size must be larger than upload max file size. also you have enctype to multipart/form-data in the form tag which is good.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输