多文件php上传

时间:2016-03-28 08:15:19

标签: php mysql file-upload

我试图将多个图像上传到服务器并将其名称存储到mysql数据库中。我无法理解脚本为什么在每个图像名称的开头追加0,1,2 ......。为什么重命名不起作用,如果我上传的图像与它所替换的名称相同的话。

DECLARE
   v_ip_address VARCHAR2(20);
BEGIN
   SELECT SYS_CONTEXT('USERENV','IP_ADDRESS') 
   INTO v_ip_address 
   from DUAL;

   IF v_ip_address = V_PROD_IP THEN
      SET_WINDOW_PROPERTY('WINDOW_NAME', 'TITLE','Prod');
   ELSIF v_ip_address = V_STAGE_IP THEN
      SET_WINDOW_PROPERTY('WINDOW_NAME', 'TITLE','Staging');
   ELSIF v_ip_address = V_DEV_IP THEN
      SET_WINDOW_PROPERTY('WINDOW_NAME', 'TITLE','Dev');
   END IF;

1 个答案:

答案 0 :(得分:2)

$ key连接到文件名

$images = array();
if(isset($_FILES['files'])){
    $errors= array();
  foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
    $file_name = $_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';
        } 
        $desired_dir="images";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);    // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"images/".$file_name);
            }else{                  //rename the file if another one exist
                $new_dir="images/".$file_name.time();
                 rename($file_tmp,$new_dir) ;       
            }
            $images[] = $file_name;     
        }else{
                print_r($errors);
        }
    }
  if(empty($error)){
    $imglinks = implode(" | ", $images);
  }
  echo $imglinks;
}