有一个HTML和一个PHP来将图像上传到服务器。我之前使用过相同的代码并且工作正常,但我不知道为什么它现在不能正常工作,
或者它可能是我使用代码的地方不合适。以下是代码。 用于从用户
获取输入的HTML<form action ="login.php" method="POST">
<label >USername :</label><br/>
<input type="text" name ="userid" /><br/>
<label >Password :</label><br/>
<input type="password" name="pid" /><br><br/>
<input type="submit" value ="Login" />
</form>
PHP for login.php
<?php
$x = 1;
while($row = mysqli_fetch_array($result)){
echo $x.'<br/>';
?>
//Form to upload the image
<form enctype="multipart/form-data" id="form" action="" method="post">
<input type="file" id="image" name="img" />
<img id="blah" src="#" alt="your image" /><br/><br/>
<input type="button" value="upload" onclick="javascript:uploadInage();" />
</form>
<script type="text/javascript">
function uploadInage()
{
var file_data = $('#image').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: "file.php",
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function (result) {
alert(result)
}
});
}
</script>
<?
echo "-------------------------------------------------------".'<br/>';
$x = $x+1;
}
print_r($stores,$stores_add,$stores_chain,$jobs);
}
?>
PHP上传文件 file.php
<?php
$imagePath = "uploads/";
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$filename = $_FILES["file"]["tmp_name"];
$time = time();
move_uploaded_file($filename, $imagePath . $time . '.' . $extension);
echo "File Uploade";
exit;
?>
请告诉我为什么这段代码不起作用。
答案 0 :(得分:2)
<form enctype="multipart/form-data" id="form" action="" method="post">
<input type="file" id="image" name="img" />
<img id="blah" src="#" alt="your image" /><br/><br/>
<input type="button" value="upload" onclick="javascript:uploadInage();" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
function uploadInage()
{
var file_data = $('#image').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: "file.php",
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function (result) {
alert(result)
}
});
}
</script>
感谢我回来了。
刚刚添加了jquery.min.js,它完全正常。
答案 1 :(得分:0)
我猜您正在使用ajax,因此您必须将此文件转换为DataUrl以通过ajax发送它
var fileInput = document.getElementById('YourInputID');
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
var imageType = /image.*/;
if (file.type.match(imageType)) {
var reader = new FileReader();
reader.onload = function(e) {
$("#myTextAreaID").val(reader.result)
}
reader.readAsDataURL(file);
}
});
它会将您的文件转换为数据网址,然后将其保存在textarea上,现在您可以通过ajax发送它
将其转换为使用php脚本的图像
convert_data_url($data_url,$idFile,$folder) {
$image = base64_decode(str_replace('data:image/png;base64,','',$data_url));
save_to_file($image,$idp,$size,$pasta);
}
function save_to_file($image,$idFile,$folder) {
if(!file_exists($folder)){
mkdir($folder);
}
$fp = fopen($folder."/".$idFile.".jpg", 'w');
fwrite($fp, $image);
fclose($fp);
}
所以你必须使用这个函数convert_data_url($ data_url,$ idFile,$ folder) 其中data_url是图像 id idFile是您想要的名称 和要保存它的文件夹。
答案 2 :(得分:0)
将此代码粘贴到login.php
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>