我正在尝试在php上上传图像,如果我单击并选择图像,我将无法上传图像,单击添加按钮后它将成功选择图像,这是行不通的。
通知:未定义索引:文件 16
中的 C:\ xampp \ htdocs \ simplesales \ add.php 中的文件
通知:未定义索引:文件 26
中的 C:\ xampp \ htdocs \ simplesales \ add.php 中的文件
通知:未定义索引: 28
行中的 C:\ xampp \ htdocs \ simplesales \ add.php 中的文件
上传文件时出错,请重试!
表格
<form role="form" id="frmCompany" enctype="multipart/form-data">
<div class="row">
<div class="col-md-12">
<div class="fileuploader fileuploader-theme-default">
<input type="hidden" name="fileuploader-list-files_" value="[]">
<input type="file" id="file" name="file" style="position: absolute; z-index: -9999; height: 0px; width: 0px; padding: 0px; margin: 0px; line-height: 0; outline: 0px; border: 0px; opacity: 0;">
<div class="fileuploader-items"><ul class="fileuploader-items-list">
</ul>
</div>
<div class="row">
<div class="col-md-12">
<div class="pull-right">
<button type="button" id="save" class="btn btn-info" onclick="validate()">Save</button>
<button type="submit" class="btn btn-default" onclick="cancel()">Cancel</button>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
jQuery
<script>
var isNew = true;
function validate() {
if ($("#frmCompany").valid())
{
var _url = '';
var _data = '';
var _method;
if (isNew == true) {
_url = 'add.php';
_data = $('#frmCompany').serialize();
_method = 'POST';
}
$.ajax({
type: _method,
url: _url,
dataType: 'JSON',
data: _data,
success: function (data) {
var msg;
if (isNew)
{
msg="image Uploaded to the folder";
}
else{
msg="Error";
}
$.alert({
title: 'Success!',
content: msg,
type: 'green',
boxWidth: '400px',
theme: 'light',
useBootstrap: false,
autoClose: 'ok|2000'
});
isNew = true;
},
error: function (xhr, status, error) {
alert(xhr);
console.log(xhr.responseText);
}
});
}
}
$('#file').fileuploader({
limit: 1,
});
</script>
PHP add.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if ($_FILES["file"]["error"] > 100) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
//echo "The file ". basename( $_FILES['file']['name']).
// " has been uploaded";
$name = $_FILES['file']['name'];
} else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>