如何在一个表单中插入4个fileupload

时间:2013-07-15 19:55:56

标签: php mysql forms file-upload pdo

我需要知道是否有可能在一个表单中有4个文件上传,当用户想要加入我们的服务时,每个文件都保存在不同的文件夹中...例如在第一个上传课程,下一个照片和最后一个两个文凭的..

以下是我正在使用的URL表格,而“documentos”标签中的所有输入类型文件都是

这是我的脚本

<?php

//----------------------------------------- start edit here ---------------------------------------------//
$script_location = "http://www.proderma.org/"; // location of the script
$maxlimit = 8388608; // maxim image limit
$folder = "uploads/foto_doc"; // folder where to save images

// requirements
$minwidth = 200; // minim width
$minheight = 200; // minim height
$maxwidth = 4608; // maxim width
$maxheight = 3456; // maxim height

$thumb3 = 1; // allow to create thumb n.3

// allowed extensions
$extensions = array('.png', '.gif', '.jpg', '.jpeg','.PNG', '.GIF', '.JPG', '.JPEG', '.docx');
//----------------------------------------- end edit here ---------------------------------------------//

    // check that we have a file
    if((!empty($_FILES["uploadfile"])) && ($_FILES['uploadfile']['error'] == 0)) {

    // check extension
    $extension = strrchr($_FILES['uploadfile']['name'], '.');
    if (!in_array($extension, $extensions)) {
        echo 'wrong file format, alowed only .png , .gif, .jpg, .jpeg
        <script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
    } else {

// get file size
$filesize = $_FILES['uploadfile']['size'];

    // check filesize
    if($filesize > $maxlimit){ 
        echo "File size is too big.";
    } else if($filesize < 1){ 
        echo "File size is empty.";
    } else {

// temporary file
$uploadedfile = $_FILES['uploadfile']['tmp_name'];

// capture the original size of the uploaded image
list($width,$height) = getimagesize($uploadedfile);

    // check if image size is lower
    if($width < $minwidth || $height < $minheight){ 
        echo 'Image is to small. Required minimum '.$minwidth.'x'.$minheight.'
        <script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
    } else if($width > $maxwidth || $height > $maxheight){ 
        echo 'Image is to big. Required maximum '.$maxwidth.'x'.$maxheight.'
        <script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
    } else {

// all characters lowercase
$filename = strtolower($_FILES['uploadfile']['name']);

// replace all spaces with _
$filename = preg_replace('/\s/', '_', $filename);

// extract filename and extension
$pos = strrpos($filename, '.'); 
$basename = substr($filename, 0, $pos); 
$ext = substr($filename, $pos+1);

// get random number
$rand = time();

// image name
$image = $basename .'-'. $rand . "." . $ext;

// check if file exists
$check = $folder . '/' . $image;
    if (file_exists($check)) {
        echo 'Image already exists';
    } else {

// check if it's animate gif
$frames = exec("identify -format '%n' ". $uploadedfile ."");
    if ($frames > 1) {
        // yes it's animate image
        // copy original image
        copy($_FILES['uploadfile']['tmp_name'], $folder . '/' . $image);

    } else {

// create an image from it so we can do the resize
 switch($ext){
  case "gif":
    $src = imagecreatefromgif($uploadedfile);
  break;
  case "jpg":
    $src = imagecreatefromjpeg($uploadedfile);
  break;
  case "jpeg":
    $src = imagecreatefromjpeg($uploadedfile);
  break;
  case "png":
    $src = imagecreatefrompng($uploadedfile);
  break;
 }

if ($thumb3 == 1){
// create third thumbnail image - resize original to 125 width x 125 height pixels 
$newheight = ($height/$width)*600;
$newwidth = 600;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagealphablending($tmp, false);
imagesavealpha($tmp,true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// write thumbnail to disk
$write_thumb3image = $folder .'/thumb3-'. $image;
 switch($ext){
  case "gif":
    imagegif($tmp,$write_thumb3image);
  break;
  case "jpg":
    imagejpeg($tmp,$write_thumb3image,100);
  break;
  case "jpeg":
    imagejpeg($tmp,$write_thumb3image,100);
  break;
  case "png":
    imagepng($tmp,$write_thumb3image);
  break;
 }
}

// all is done. clean temporary files
imagedestroy($src);
imagedestroy($tmp);
echo "<script language='javascript' type='text/javascript'>window.top.window.formEnable();</script>
<div class='clear'></div>";
      }
    }
  }
}
    // database connection
    include('include/config.php');
$stmt = $conn->prepare('INSERT INTO INGRESOS (nombre,dui,nit,direccion,curriculum,pais,departamento,ciudad,telefono,email,universidad,diploma,jvpm,especializacion,diploma_esp,foto,website,contacto) 
VALUES (:nombre,:dui,:nit,:direccion,:curriculum,:pais,:departamento,:ciudad,:telefono,:email,:universidad,:diploma,:jvpm,:especializacion,:diploma_esp,:foto,:website,:contacto)');
$stmt->execute(array(':nombre' => $nombre,':nit' => $nit,':direccion' => $direccion,':curriculum' => $path,':pais' => $pais,':departamento' => $departamento,':ciudad' => $ciudad,':departamento' => $departamento,':ciudad' => $ciudad,':telefono' => $telefono,':email' => $email,':universidad' => $universidad,':diploma' => $path1,':jvpm' => $jvpm,':especializacion' => $especializacion,':diploma_esp' => $path2,':foto' => $write_thumb3image,':website' => $website,':contacto' => $contacto));
    echo 'Afiliación ingresada correctamente.';                             
 $dbh = null;   
      }
        // error all fileds must be filled
    } else {
        echo '<div class="wrong">You must to fill all fields!</div>'; }
?>

我需要的文件只保存路径,所以我有这个:

:curriculum' => $path
:diploma' => $path1
:diploma_esp' => $path2
:foto' => $write_thumb3image

我不知道这些是否可能......我希望它可以。

最诚挚的问候!

1 个答案:

答案 0 :(得分:2)

包含多个文件字段

<input type="file" name="file[0]" />
<input type="file" name="file[1]" />
<input type="file" name="file[2]" />
<input type="file" name="file[3]" />

围绕保存脚本包装foreach

// set up your paths
$paths=array($path, $path1, $path2, $write_thumb3image);
//            0       1       2        3

// use them as your loop
foreach ($paths as $i=>$path){ // <- use an index $i

    // *** save to $path.'/'.$your_image_name.$image_ext

    // as Phillip rightly pointed out, $_FILES is different
    // so using your index, pick out the bits from the $_FILES arary
    $name=$_FILES['file']['name'][$i];
    $type=$_FILES['file']['type'][$i];
    $tmp_name=$_FILES['file']['tmp_name'][$i];
    $size=$_FILES['file']['size'][$i];
    $error=$_FILES['file']['error'][$i];
    $extension = strrchr($name, '.'); // NB the file name does not mean that's the true extension
    ....
}

未测试