将单张图片上传转换为多张图片上传?

时间:2016-06-06 13:13:01

标签: php mysql image file-upload upload

为此特定问题开始新线程......

以下是我在其他网站上运行的代码...它允许您上传1张图片:

  $photo=($_FILES['photo']['name']);

  //Writes the photo to the server
  if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {

  //Tells you if its all ok
  echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
  }
  else {
  //Gives and error if its not
  echo "Sorry, there was a problem uploading your file.";

以下是我在当前网站上用于上传多张图片的代码....目前尚未正常工作。实际上,在表单的一个部分中,我上传的是徽标图片,这只是一张图片。然后在表单的另一部分中,我上传了多个屏幕截图。不确定如何使代码工作:

  //code for uploading multiple screenshots

  // check files are set or not
  if(isset($_FILES['files'])){

$errors= array();
$desired_dir="images/"; // replace with your directory name where you want to store images
// getting files 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];  
    // checking file size (because i use it)
}
    if($file_size > 8097152){
        $errors[]='File size must be less than 8 MB'; // change or remove it
    }       

    $FILE_NAME=time().$FILE_NAME; // for creating unique file name
    if(empty($errors)==true){
        // moving files to destination
        $move=move_uploaded_file($file_tmp,$desired_dir.$FILE_NAME);
        // you can direct write move_uploaded files method in bellow if condition
        if($move)
        {
            // inserting data into database
            mysql_query("INSERT into Colleges (`EMP_CODE`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES ('$codes','$file_name','$file_size','$file_type')"); // inserting data if file is moved
            echo "The file ".$FILE_NAME." has been uploaded"; // only for debugging
        }
        else{
            echo $FILE_NAME."is not uploaded"; // use this for debugging otherwise remove it
        }

    }
    else{
        echo $errors;
    }
        }
}
if(empty($error)){
    echo "All attachments are uploaded successfully"; // your success message/action
}

  //end of code for uploading multiple screenshots

令我困惑的是,SINGLE IMAGE UPLOAD代码仅使用一个变量“photo”,而MULTIPLE IMAGE UPLOAD使用多个变量。该代码基于this tutorial。不太确定如何让这3个变量起作用,这是一个更长的代码,让我更加困惑。

所以基本上我要问的是:如何让多图像上传代码工作,以及如何使用单个图像上传代码?

0 个答案:

没有答案