PHP:如何在迭代$ _FILES全局变量后将“当前$ _FILE”传递给另一个函数?

时间:2014-10-26 03:32:24

标签: php file-upload

如果我提交一个包含多个文件的表单,我怎样才能逐个执行某些文件后上传每个文件?

1 个答案:

答案 0 :(得分:1)

将这样的内容添加到您的PHP页面

//upload image 1
if ($filename1<>"") {
    $filename = $filename1;
    $file = 'file1';
    $temp = explode(".", $_FILES["file1"]["name"]);
    include "upload_file.php";
    $updateimageurl = mysql_query("update yacht set image1 = '$newfilename' where yachtid = '$yachtid'");
}
//upload image 2
if ($filename2<>"") {
    $filename = $filename2;
    $file = 'file2';
    $temp = explode(".", $_FILES["file2"]["name"]);
    include "upload_file.php";
    $updateimageurl = mysql_query("update yacht set image2 = '$newfilename' where yachtid = '$yachtid'");
}

然后名为“upload_file.php”的文件看起来应该是这样的(如果您希望它在不同的文件名上验证,请更改验证部分。)此外,这会将文件重命名为随机名称,然后再将其保存到您的位置);

<?php

$length = 30;
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
$code = "";
for ($p = 0; $p < $length; $p++) {
    $pos = mt_rand(0, strlen($characters)-1);
    $code .= $characters{$pos};
}

$parts = explode('.',$filename);
$extension= end($parts);

$newfilename=$code .".".$extension;
$success = 0;



$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end($temp);

if ((($_FILES[$file]["type"] == "image/gif") || ($_FILES[$file]["type"] == "image/jpeg") || ($_FILES[$file]["type"] == "image/jpg")
|| ($_FILES[$file]["type"] == "image/pjpeg") || ($_FILES[$file]["type"] == "image/x-png") || ($_FILES[$file]["type"] == "image/png"))
&& ($_FILES[$file]["size"] < 1000000) && in_array($extension, $allowedExts)) {


    $filenamepng = "./images/yacht/".$code.".png";
    $filenamegif = "./images/yacht/".$code.".gif";
    $filenamejpeg = "./images/yacht/".$code.".jpeg";
    $filenamejpg = "./images/yacht/".$code.".jpg";
    $filenamepjpeg = "./images/yacht/".$code.".pjpeg";
    $filenamexpng = "./images/yacht/".$code.".x-png";

    if (file_exists($filenamepng)||file_exists($filenamegif)||file_exists($filenamejpeg)||file_exists($filenamejpg)||file_exists($filenamepjpeg)||file_exists($filenamexpng)) {
      if (file_exists($filenamepng)) {
        $dir = './images/yacht/';
        $filename = $dir.$code.'.png';
        unlink($filename);
      }
      if (file_exists($filenamegif)) {
        $dir = './images/yacht/';
        $filename = $dir.$code.'.gif';
        unlink($filename);
      }
      if (file_exists($filenamejpeg)) {
        $dir = './images/yacht/';
        $filename = $dir.$code.'.jpeg';
        unlink($filename);
      }
      if (file_exists($filenamejpg)) {
        $dir = './images/yacht/';
        $filename = $dir.$code.'.jpg';
        unlink($filename);
      }
      if (file_exists($filenamepjpeg)) {
        $dir = './images/yacht/';
        $filename = $dir.$code.'.pjpeg';
        unlink($filename);
      }
      if (file_exists($filenamexpng)) {
        $dir = './images/yacht/';
        $filename = $dir.$code.'.x-png';
        unlink($filename);
      }
    }

      move_uploaded_file($_FILES[$file]["tmp_name"],
      "images/yacht/" . $newfilename);
      //echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

    unset($code);
    $success = 1;
}
else
{
    $error = "Your image is over 1mb OR is not in an accepted format; gif, jpeg, jpg, pjpeg, x-png, or png. Please try again.";
}
?>

www.clubtray.com www.clubtray-clubmembershipsoftware.com