如何使用PHP上传照片并在文件夹和页面中显示?

时间:2016-01-01 02:18:09

标签: php html

我正在尝试上传照片并在文件夹中显示。然后,当您转到该页面时,它将显示已上传的所有照片。我正在为这个项目使用PHP和HTML。 希望你现在了解我的项目。

这是我的php代码

<?php


  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"uploads/".$file_name);
     echo "Success";
    }
    else{
       print_r($errors);
    }
 }
   ?>
<html>
<link rel="stylesheet" type="text/css" href="css/main1.css"/>
 <body>
 <?php include("includes/navigation.php"); ?>   
  <form action="" method="POST" enctype="multipart/form-data">
     <input type="file" name="image" />
     <input type="submit"/>
  </form>

 </body>
</html>

要在我的页面上显示:

PHP:

 <?php
   $dirname = "uploads/";
   $images = glob($dirname."*.*");
  ?>

<?php
foreach($images as $image) {
echo '<img src="'.$image.'" /><br /><br />';
} 
?>

帮助将不胜感激,

1 个答案:

答案 0 :(得分:1)

<?php
$dir = './';
$name = $_FILES['image']['name'];
$fullpath = $dir.$name;
$extension = pathinfo($name, PATHINFO_EXTENSION);
$tmpFilename = pathinfo($name, PATHINFO_FILENAME);
$i = 1;

while(file_exists($fullpath)){
    $_FILES['image']['name'] = $tmpFilename.'('.$i.')'.$extension;
    $fullpath = $dir.$_FILES['image']['name'];
    $i++;
 }
    move_uploaded_file($_FILES('image']['tmp_name'], $fullpath);
?>

在您的表单操作属性中:

upload_photo.php