[英]Image display using PHP and Javascript
Here is the plan: I will click the submit button then display the image while saving the image on a folder. 这是计划:我将单击提交按钮,然后在将图像保存到文件夹的同时显示图像。 The problem is that I can't display and save the image. 问题是我无法显示和保存图像。
Here is my code: 这是我的代码:
<?php
$UPLOADDIR = "images/";
if(isset($_POST['Submit'])){
foreach ($_FILES as $key => $value){
$image_tmp = $value['tmp_name'];
$image_type=$value['type'];
$image = $value['name'];
$image_file = "{$UPLOADDIR}{$image}";
move_uploaded_file($image_tmp,$image_file)
echo " $(function () {
$(':file').change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
$('#myImg').attr('src', e.target.result);
};
</script> ";
?>
<img id="myImg" src="#" alt="your image" />
Thank you for the response 感谢您的答复
I think you miss the Script opening tag
and some mistakes. 我想您会错过Script opening tag
和一些错误。 try this 尝试这个
<?php
$UPLOADDIR = "images/";
if(isset($_POST['Submit'])){
foreach ($_FILES as $key => $value){
$image_tmp = $value['tmp_name'];
$image_type=$value['type'];
$image = $value['name'];
$image_file = "{$UPLOADDIR}{$image}";
move_uploaded_file($image_tmp,$image_file);
echo "<script type='text/javascript'>$(function () {
$(':file').change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
$('#myImg').attr('src', e.target.result);
};
</script>";
}
}
?>
<img id="myImg" src="#" alt="your image" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.