我想将图像上传到目录,但是表单存在问题。当表单被提交时,它被重定向到表单动作中指定的php文件,但似乎没有意识到要提交的表单。正在提交图像上传表单PHP
HTML
<html>
<form action="profileimg.php" method="post" enctype="multipart/form-data">
<label for="chooseimg" class="profile-pic-change"><i class="material-icons" style="color: rgb(255, 255, 255);">add_a_photo</i></label>
<input type="file" name="image" accept="image/gif, image/jpeg, image/png" id="chooseimg" value="Image" onchange="javascript:this.form.submit();" />
</form>
</html>
(我使用普通的提交按钮也试过;没有区别)
PHP
<?php
include 'connect.php';
$target_dir = "Users/";
$name = $_FILES["image"]["name"];
$name = md5($name);
$name = $name . uniqid($name);
$path = $_FILES["image"]["name"];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$name = $name.".".$ext;
$target_file = $target_dir . $name;
if(isset($_POST['submit']))
{
move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
$SQL = "INSERT INTO Users (image) VALUES ('$target_file')";
$result = mysql_query($SQL);
if($result)
{
echo "<script type='text/javascript'>alert('success!')</script>";
}
else
{
echo "<script type='text/javascript'>alert('failed!')</script>";
}
}
else
{
echo 'didnt submit';
}
?>
我加了没收到后提交的回声没有提醒,看看这是否是结果,这就是我得到的。它重定向到该页面,只显示没有提交。任何想法,为什么这可能是?
在此先感谢。
这是哪里的输入? 'if(isset($ _ POST ['submit']))'或JS。为什么不检查真正的错误? –
请查阅以下链接http://php.net/manual/en/function.mysql-error.php和http://php.net/manual/en/function.error-reporting.php 并将其应用到您的代码,然后回来告诉他们是什么,如果有的话。 –
输入是文件,并提交应该发生在文件选择,但我也尝试提交按钮。都没有工作。试图将这些错误报告功能应用到我的代码中,但除了内部服务器错误 – Codedstuff