duanan6043 2013-06-07 02:26
浏览 21

每个上传多张照片并将它们分配给数据库中的一个用户ID

i am building an ad site that allows users to upload multiple photos per ad, how do i get this to work? i am new to php

<?php echo $error_msg; ?>

<?php
    include_once "connection.php";

    if(isset($_FILES['files'])){

    $errors= array();

    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  

        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }

        $sql = mysql_query("INSERT INTO upload_data (`postid`,`file_name`,`file_size`,`file_type`) VALUES('$postid','$file_name','$file_size','$file_type')")or die (mysql_error());

        $desired_dir="uploads";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);        
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{                                  
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir);                
            }
            mysql_query($sql);          
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
    echo "Success";
        }
    }
?>
  • 写回答

1条回答 默认 最新

  • donglin7979 2013-06-07 08:55
    关注

    The most common problem encountered when trying to upload multiple files is the upload_max_filesize and the max_post_size in your php.ini file.

    If you have no problems uploading a series of very small images, then this will be your problem.

    This quesion: php file uploads' size discusses the issue effectively.

    You should also consider echoing the $_FILES['files']['error'] which will often be instructive. The php manual explains the codes.

    Don't forget to check that you have included the correct call in your HTML FORM element too:

    <form method='POST' enctype='multipart/form-data' action='myfile.php'>
    

    Make sure you have not specified an undersized max_file_size in your HTML form too.

    I suggest that you don't prohibit large file uploads but rather you resize any overlarge images in your script. This will help your users substantially.

    I hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答