在PHP中提交到永久文件夹后,如何显示或预览上传的图像?

在PHP中提交到永久文件夹后,如何显示或预览上传的图像?,php,upload,Php,Upload,如何创建一个php页面,在其中显示上传到永久文件夹的图像 (在表格中显示图像) 这是form.php <form dir="rtl" action="upload_file.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" id="file" ><br> <input type="submit" name="submit" value=

如何创建一个php页面,在其中显示上传到永久文件夹的图像

(在表格中显示图像)

这是form.php

<form dir="rtl" action="upload_file.php" method="post"
enctype="multipart/form-data">
  <input type="file" name="file" id="file" ><br>
  <input type="submit" name="submit" value="upload">
</form>


这是upload_file.php

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$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"] < 1048576)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo " thanks";  
    move_uploaded_file($_FILES["file"]["tmp_name"],
                       "upload/" . $_FILES["file"]["name"]);


    }
  }
else
  {
  echo "wrong";
  }




?>

非常感谢基本示例,但是如果您建议使用,它会给您带来更多的灵活性

<?php

$dir = 'images';

$extensions = array("gif","png","jpg");

foreach( $extensions as $ext ) $images = ( isset( $images ) )? array_merge( $images, glob("$dir/*.$ext") ) : glob("$dir/*.$ext");

foreach( $images as $image ) echo "<img src=\"$image\" />";

?>

没有太多的解释(您的问题相当模糊……)没有图像的具体位置(与正在执行的脚本相关),也没有图像在页面上的显示方式(表列名等)

查看phpdirname(_FILE_uu)目录分隔符以查找文件夹的位置-在这种情况下(当脚本位于
www/htdocs
中web服务器的
project
文件夹中时-取决于您的服务器包)。此示例引用的是
project/images
文件夹

$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'images';
//get contents of the folder
$foldercontents = array_diff(scandir($dir, 1), array('..', '.'));
//for each of the folder contents in the array
foreach($foldercontents as $key => $file)
{
    //get extension of the current file - name of file after last "." (the file extension)
    $file_ext = end(explode(".", $file));
    //allowed extensions
    $extensions = array("gif","png","jpg");
    //if the extension of the current file is allowed
    if(in_array($file_ext, $extensions))
    {
        //place the image on the page
        echo "<img src='images/'.$file/>";
    }
}
$dir=dirname(_文件)。目录“”分隔符图像';
//获取文件夹的内容
$foldercontents=array_diff(scandir($dir,1),array(“…”,“);
//对于数组中的每个文件夹内容
foreach($foldercontents作为$key=>$file)
{
//获取当前文件的扩展名-最后一个“.”之后的文件名(文件扩展名)
$file_ext=end(分解(“.”,$file));
//允许的扩展
$extensions=数组(“gif”、“png”、“jpg”);
//如果允许使用当前文件的扩展名
if(在数组中($file\u ext,$extensions))
{
//将图像放在页面上
回声“;
}
}

如果我能提供更多帮助,请随时回复

不清楚你在问什么。。。您是在寻找重定向到另一个显示上载图像的页面,还是试图将URL发送到其他地方的上载图像?