首页 文章

单个图像到多个图像上传

提问于
浏览
0

我有跟踪脚本,上传和图像和缩略图到目录,并将 img 名称(img 和拇指)存储到数据库。

我最初在 Google 上的某个地方发现了这个脚本,但是,但是,但是现在我需要能够上传多个图像...它可以正常处理一个图像。

<?php 
//error_reporting(0);

$change="";
$abc="";

 define ("MAX_SIZE","400");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;

 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
    $image =$_FILES["file"]["name"];
    $uploadedfile = $_FILES['file']['tmp_name'];

    if ($image) 
    {

        $filename = stripslashes($_FILES['file']['name']);

        $extension = getExtension($filename);
        $extension = strtolower($extension);

 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
        {

            $change='<div class="msgdiv">Unknown Image extension </div> ';
            $errors=1;
        }
        else
        {

 $size=filesize($_FILES['file']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
    $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
    $errors=1;
}

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);

$newwidth=60;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=25;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

$filename = "images/". $_FILES['file']['name'];

$filename1 = "images/small". $_FILES['file']['name'];

imagejpeg($tmp,$filename,100);

imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}

}

//If no errors registred, print the success message
 if(isset($_POST['Submit']) && !$errors) 
 {

    mysql_query("INSERT INTO `imgs` (`id` ,`user_id` ,`img_name`) VALUES (NULL ,  '$userID',  'img1.jpg');");
    $change=' <div class="msgdiv">Image Uploaded Successfully!</div>';
 }

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta content="en-us" http-equiv="Content-Language">
  </head><body>
<?php echo $change; ?> 

              <div id="posts">
              <form method="post" action="" enctype="multipart/form-data" name="form1">
              <input size="25" name="file" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/>
              <input type="submit" id="mybut" value="Upload" name="Submit"/>
              </form>
              </div>
</body></html>

对于多个图像我知道我可以将 html 部分更改为 foreach 循环,如:

<form method="post" action="" enctype="multipart/form-data" name="form1">
            <?php
$allow_upload = 10; 
for($i=0; $i<$allow_upload; $i++) { 
            echo '<input size="25" name="file[]" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/>'; 
        }
?>          
<input type="submit" id="mybut" value="Upload" name="Submit"/>
</form>

但我还没有能够计算出如何以及在何处放置一个 foreach 循环并使其余部分适应多个图像。

我的数据库设置会像 id,userID,big img,small img。任何帮助将不胜感激,我准备开始拉我的头发!

2 回答

  • 0

    如果你想循环多个文件输入元素,那么除非每个输入元素都有不同的名称,否则你需要告诉它它是一个数组。

    for($i=0; $i<$allow_upload; $i++) { 
        echo '<input size="25" name="file[]" type="file"'/>; 
    }
    

    如果要从一个输入元素中选择多个文件

    <input name="filesToUpload[]" type="file" multiple/>
    
  • 0

    您可以使用单个输入文件并将所有文件保存在数组中,而不是使用多个输入文件,并使用 foreach 将每个文件保存到文件夹。

    <?php
    if($_SERVER["REQUEST_METHOD"] == "POST") {
    
    foreach($_FILES['allimages'] as $files) {
    var_dump($files);
        }
    }
    
    ?>
    
    <form id="fileupload" enctype="multipart/form-data" method="post">
    <input type="file" name="allimages[]" id="allimages" multiple><!--Use CTRL key to upload multiple images -->
    <input type="submit" name="submit" id="submit">
    </form>
    

    你需要按住 cntrl 键才能上传多个文件。

相关问题