如何创建一个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";
}
?>
非常感谢
答案 0 :(得分:0)
基本glob示例但是如果建议使用Directory Iterator,则可以提供更多灵活性
<?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\" />";
?>
答案 1 :(得分:0)
解释的方式并不多(你的问题很模糊......)没有关于图像所在位置的细节(与正在执行的脚本有关)或者你希望它们在页面上显示的方式(表格列)名字等)
查看php dirname(_ FILE _), DIRECTORY_SEPARATOR 以查找文件夹的位置 - 在这种情况下(当脚本时)位于project
内的Web服务器的www / htdocs
文件夹中 - 具体取决于您的服务器pacckage)。这是引用此示例的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/>";
}
}
如果我能提供更多帮助,请随时回复!