[英]Uploading and displaying images in php
I am buiding a website which will be having a form like this我正在建立一个网站,它将有这样的表格
<form action="demo.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<div class="col-12 text-center tm-submit-container">
<button type="submit" href="#" class="btn btn-primary tm-btn-submit">Submit</button>
</div>
</form>
here i need to upload a image in this page and show it in demo.php page(redirects when submitted) demo.php这里我需要在这个页面上传一张图片并在 demo.php 页面中显示它(提交时重定向) demo.php
<body>
<p style="text-align: right;" id="time"></p>
<h1 style="text-align: center;">
welcome
</h1>
</body>
can someone help me how to do it?i tried tutorials in w3 schools but that didnot work.有人可以帮我怎么做吗?我在 w3 学校试过教程,但没有奏效。
View Page:-查看页面:-
<form method="post" action="" enctype='multipart/form-data'>
<input type="file" name="fileToUpload" id="fileToUpload">
<div class="col-12 text-center tm-submit-container">
<button type="submit" name="but_upload" class="btn btn-primary tm-btn-submit">Submit</button>
</div>
</form>
insertion Code:-插入代码:-
<?php
include("config.php");
if(isset($_POST['but_upload'])){
$name = $_FILES['fileToUpload']['name'];
$target_dir = "upload/"; // create a folder in a directory.
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif");
// Check extension
if( in_array($imageFileType,$extensions_arr) ){
// Insert record
$query = "insert into images(images) values('".$name."')";
mysqli_query($con,$query);
// Upload file
move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_dir.$name);
}
}
?>
View Image Code:-查看图片代码:-
<?php
$sql = "select * from images";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
$image = $row['image'];
$image_src = "upload/".$image;
<img src='<?php echo $image_src; ?>' >;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.