我正在使用
的图库上传功能整个图库功能包含3个部分:
表格为 view_create-char.php
<form id='gallery_upload' method='post' enctype='multipart/form-data'>
<input type='file' name='file' id='file'>
<input type='hidden' name='name' value='".$_GET['c']."' id='owner'>
<input type='button' class='create_submit' value='Upload'
id='add_image_gallery'>
</form>";
Ajax脚本作为 main.js
$(document).ready(function(){
$("#add_image_gallery").click(function(){
var fd = new FormData();
var files = $('#file')[0].files[0];
var name = $('#owner').attr("value");
fd.append('file',files);
fd.append('name',name);
$.ajax({
url: 'includes/gallery_upload.php',
type: 'post',
data: fd,
contentType: false,
processData: false,
success: function(response){
if(response != 0){
alert('file uploaded');
}else{
alert('file not uploaded');
}
},
});
});
});
PHP脚本作为 gallery_upload.php
<?php
include 'dbh.inc.php';
$owner = $_POST['name'];
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'gif');
$fileNameNew = uniqid('', true).'.'.$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
$sql = "INSERT INTO
characters_gallery (owner, image) VALUES ('$owner',
'$fileNameNew');";
$result = mysqli_query($conn, $sql);
目标是使用Ajax上传图像并将两个值插入character_gallery-table中。首先,表的所有者,然后是PHP脚本生成的实际图像的名称。 这有效。
但是 move_uploaded_file()不会将实际文件移动到指定的文件夹/ uploads。尽管我数据库中的表填充了两个值 $ owner 和 $ fileNamenew ,但它仍然为空。令我感到困惑的是,在我使用此Ajax脚本之前,此PHP脚本将文件移动到上载文件夹时工作良好。它不适用于脚本。
感谢您的帮助
编辑---对有关SQL注入的警告的反应:
我不确定是否像我应该的那样使用此编辑,但这是附加信息。如果我输入错了,请删除我的修改。
我试图添加准备好的语句,并且想知道是否消除了SQL注入的更大风险:
<?php
include_once 'characterfunctions.inc.php';
include 'dbh.inc.php';
$owner = mysqli_real_escape_string($conn, $_POST['name']);
$file = mysqli_real_escape_string($conn, $_FILES['file']);
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'gif');
$fileNameNew = uniqid('', true).'.'.$fileActualExt;
$fileDestination = '../uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
$sql = "INSERT INTO characters_gallery (owner, image) VALUES (?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL Error";
} else {
mysqli_stmt_bind_param($stmt, "ss", $owner, $fileNameNew);
mysqli_stmt_execute($stmt);
}
$result = mysqli_query($conn, $sql);
答案 0 :(得分:1)
首先,跟踪move_uploaded_file
的结果,如果为false
,则不要执行下一步。
关于失败的可能原因,它是:
uploads
目录的权限。$_FILES['file']
是无效文件-因此请检查is_uploaded_file($fileTmpName)
并检查变量$_FILES['file']['error']
答案 1 :(得分:0)
尝试将../添加到$ fileDestination中:
$fileDestination = '../uploads/'.$fileNameNew;
您似乎将照片移动到包含/上传,但在笔记中却说上传位于 /上传。 / p>