0

我有以下脚本,可以将图像和缩略图上传到目录并将 img 名称(img 和 thumb)存储到数据库中。

我最初在谷歌搜索时在网络上的某个地方找到了这个脚本,但是现在我需要能够上传不止一张图片......它适用于一张图片。

<?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。任何帮助将不胜感激,我准备开始拔头发了!!

4

2 回答 2

0

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

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

如果你想从一个输入元素中选择多个文件,那么

<input name="filesToUpload[]" type="file" multiple/>
于 2012-05-07T05:20:25.147 回答
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 键来上传多个文件。

于 2012-05-07T06:11:01.447 回答