我有一个用户可以上传文件的网页。现在,代码可以成功地将文档上载到服务器上的目录,而不会出现任何问题。但是,我需要将文档作为一个新的插入行上载到MySQL表中,然后将文档作为链接显示在原始网页上。但是,每次我尝试上传到MySQL时,它都会失败,我不知道为什么。在调试模式下收到0个错误,我可以登录并成功连接到数据库。我的查询失败了,但我可以在MySQL中成功运行查询,没有错误。
我的代码:
HTML格式:
<body>
<br/>
<div id="bodydiv">
<fieldset id='title'>
<span style='color:aliceblue'>Uploaded SG Documents</span>
</fieldset>
<br/>
<fieldset id='docTypeWO'>
<span>Scanned Work Orders:</span>
<div id='responseWO'>
</div>
</fieldset>
<br/>
<fieldset id='docTypeCS'>
<span>Cut Sheets:</span>
<div id='responseCS'>
</div>
</fieldset>
<br/>
<fieldset id='docTypeOther'>
<span>Others:</span>
<div id='responseOther'>
</div>
</fieldset>
<br/>
<form name="sgFileUpload" id="sgFileUpload" action='sg_addupload.php' method="POST" enctype="multipart/form-data">
<fieldset id='uploadBtnField'>
<input type="hidden" name="MAX_FILE_SIZE" value="50000000"/>
<input type='file' name='searchFile' id='searchFile' multiple>
<input type='submit' name='startUpload' id='startUpload' value='Upload'>
<!-- <input type='reset' name='cancelUpload' id='cancelUpload' value="Cancel Upload">
<input type='button' name='deleteFile' id='deleteFile' value='Delete'> -->
</fieldset>
<!-- The table listing the files available for upload/download -->
<table><tbody></tbody></table>
</form> <!-- End Form Input -->
</div>
</body>
</html>
我的AJAX:
j('#startUpload').on('click', function() {
var file_data = j('#searchFile').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
alert(form_data);
j.ajax({
url: 'sg_addupload.php', // point to server-side PHP script
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(data){
j('#responseWO').html(data); // display response from the PHP script, if any
}
});
});
我的PHP:
include('inc.php');
//This section works successfully to upload to a directory on the server.
if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
}
//This section fails...
//This is the directory where images will be saved
$target = "uploads/";
$target = $target . basename( $_FILES['file']['name']);
//This gets all the other information from the form
$fileName = basename( $_FILES['file']['name']);
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
//Writes the Filename to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded, and your information has been added to the directory";
//connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if(mysqli_connect_errno() ) {
printf('Could not connect: ' . mysqli_connect_error());
exit();
}
$conn->select_db($dbname);
if(! $conn->select_db($dbname) ) {
echo 'Could not select database. '.'<BR>';
}
//Writes the information to the database
mysqli_query("INSERT INTO sg_uploads(sgref,file,type,size,content,doctype) VALUES('4','$fileName','$fileType','$fileSize','$content','Other')");
} else {
//Gives an error if its not
echo "Sorry, there was a problem uploading your file.";
}
感谢大家的帮助。谢谢您!
最佳答案
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
if(isset($_POST['btn-upload'])) {
include '../includes/dbcon.php';
$file = $_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$title = mysqli_real_escape_string($con, $_POST['title']);
$keywords = mysqli_real_escape_string($con, $_POST['keywords']);
$categ = mysqli_real_escape_string($con, $_POST['categ']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$art_info = mysqli_real_escape_string($con, $_POST['art_info']);
$folder="../uploads_art_jou/";
$allowed = array('pdf','doc' ,'docx');
$file = $_FILES['file']['name'];
$ext = pathinfo($file, PATHINFO_EXTENSION);
$file = $folder."$file";
//$location =mysqli_real_escape_string ($con, $_POST['location']);
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
//checks file extension for images only
if(!in_array($ext,$allowed) )
{
?>
<script>
alert('file extension not allowed');
window.location.href='art_jou_add.php?file_type_not_allowed_error';
</script>
<?php
}
//check whether file exist in said folder
elseif (file_exists($file))
{
?>
<script>
alert('file already exist');
window.location.href='art_jou_add.php?file_exist';
</script>
<?php
}
//if file does not exist, move it to folder and save details to table
else(move_uploaded_file($file_loc,$folder.$file));
{
$sql="INSERT INTO art_jou(file,type,size,title,keywords,categ,email,art_info)
VALUES('$file','$file_type','$file_size','$title','$keywords','$categ','$email','$art_info')";
mysqli_query($con,$sql);
echo "it is done";
?>
<?php
}
}
?>
这对我有用