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.

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改