我是 php 的初学者,现在正在用 php 做一个项目。我想上传图片(最多四个图片文件)。我使用以下代码上传图片。
<?php if(isset($_POST['submit'])) {
$count=count($_FILES["images"]["name"]);
for($i=0;$i<$count;$i++) { if ((($_FILES["images"]["type"][$i] == "image/gif") || ($_FILES["images"]["type"][$i] == "image/jpeg") || ($_FILES["images"]["type"][$i] == "image/pjpeg")) && ($_FILES["images"]["size"][$i] < 100000)) {
if ($_FILES["images"]["error"][$i] > 0) { echo "File Error : " . $_FILES["images"]["error"][$i] . "<br />"; } else { echo "Upload File Name: " . $_FILES["images"]["name"][$i] . "<br />"; echo "File Type: " . $_FILES["images"]["type"][$i] . "<br />"; echo "File Size: " . ($_FILES["images"]["size"][$i] / 1024) . " Kb<br />";
if (file_exists("public/images/".$_FILES["images"]["name"][$i] )) { echo "<b>".$_FILES["images"]["name"][$i] . " already exists. </b>"; } else {
move_uploaded_file($_FILES["images"]["tmp_name"][$i] ,"public/images/". $_FILES["images"]["name"][$i] );
echo "Stored in: " . "public/images/" . $_FILES["images"]["name"][$i] ."<br />"; ?> Uploaded File:<br> <img src="public/images/<?php echo $_FILES["images"]["name"][$i] ; ?>" alt="Image path Invalid" > <?php } } }else { echo "Invalid file detail ::<br> file type ::".$_FILES["images"]["type"][$i] ." , file size::: ".$_FILES["images"]["size"][$i] ; } } }?>