gpt4 book ai didi

javascript - 无法使用 $_FILES 过程在 PHP 中上传图像

转载 作者:行者123 更新时间:2023-11-29 21:53:34 26 4
gpt4 key购买 nike

我正在使用 PHP $_FILE 程序将图像上传到特定文件夹,但没有任何反应,我也尝试使用一些片段,但似乎没有任何效果,我正在使用 PHP 和 JavaScript 来执行此操作,

这是 PHP 代码:

<?php

if (isset($_POST['postImage']))
{
$validextensions = array(
"jpeg",
"jpg",
"png",
"gif"
);
$temporary = explode(".", $_FILES["postImage"]["name"]);
$file_extension = end($temporary);
if ((($_FILES["postImage"]["type"] == "../news/img/.png") || ($_FILES["postImage"]["type"] == "../news/img/.jpg") || ($_FILES["postImage"]["type"] == "../news/img/.jpeg") || ($_FILES["postImage"]["type"] == "../news/img/.gif")) && in_array($file_extension, $validextensions))
{
if ($_FILES["postImage"]["error"] > 0)
{
echo "Return Code: " . $_FILES["postImage"]["error"] . "<br/><br/>";
}
else
{
echo "<span>Your File Uploaded Succesfully...!!</span><br/>";
echo "<br/><b>File Name:</b> " . $_FILES["postImage"]["name"] . "<br />";
echo "<b>Type:</b> " . $_FILES["postImage"]["type"] . "<br />";
echo "<b>Temp file:</b> " . $_FILES["postImage"]["tmp_name"] . "<br />";
if (file_exists("../news/img/" . $_FILES["postImage"]["name"]))
{
echo $_FILES["postImage"]["name"] . " <b>already exists.</b> ";
}
else
{
move_uploaded_file($_FILES["postImage"]["tmp_name"], "../news/img/" . $_FILES["postImage"]["name"]);
echo "<b>Stored in:</b> " . "../news/img/" . $_FILES["postImage"]["name"];
}
}
}
else
{
echo "<span>***Invalid file Size or Type***<span>";
}
}

?>

JavaScript 代码:

<script>
$(document).ready(function() {
// Function for Preview Image.
$(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) {
$('#message').css("display", "none");
$('#preview').css("display", "block");
$('#previewimg').attr('src', e.target.result);
};
// Function for Deleting Preview Image.
$("#deleteimg").click(function() {
$('#preview').css("display", "none");
$('#file').val("");
});
// Function for Displaying Details of Uploaded Image.
$("#submit").click(function() {
$('#preview').css("display", "none");
$('#message').css("display", "block");
});
});
</script>

HTML代码:

 <form action="" method="post">  
<div id="message"></div>

<input id="postImage" type="file" name="postImage" placeholder="Upload Image" value="">

<input type="submit">

<br>
<br>

<div id="preview">
<img id="previewimg" src="">
</div>
</form>

最佳答案

换一行:

 <form action="" method="post" enctype="multipart/form-data">

你错过了

enctype="multipart/form-data" 

用于文件上传。

关于javascript - 无法使用 $_FILES 过程在 PHP 中上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27688411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com