【发布时间】:2017-01-18 11:21:37
【问题描述】:
PHP 代码
$target = "upload/";
$nameF = "";
$targetImage = "upload/";
$nameI = "";
if (!empty($_FILES['fileUP']['name'])) {
print_r("ce il file");
$target = $target . basename($_FILES['fileUP']['name']);
$nameF = $_FILES['fileUP']['name'];
if (!move_uploaded_file($_FILES['fileUP']['tmp_name'], $target)) {
echo -1;
}
}
if (!empty($_FILES['imageUP']['name'])) {
$targetImage = $targetImage . basename($_FILES['imageUP']['name']);
$nameI = $_FILES['imageUP']['name'];
if (!move_uploaded_file($_FILES['imageUP']['tmp_name'], $targetImage)) {
echo -1;
}
}
$title = $_POST['title'];
$admin = $_POST['admin'];
$content = $_POST['content'];
$sql = "INSERT INTO news (title,admin,content,img,file) values('$title','$admin','$content','$nameI','$nameF')";
$result = $conn->query($sql) or die(mysql_error());
if ($result === TRUE) {
echo 1;
} else {
echo -1;
}
表格
<form enctype="multipart/form-data" id="insert" class="bs-example bs-example-form" method="POST">
<div class="input-group">
<span class="input-group-addon">Titolo</span>
<input id="title" name="title" type="text" class="form-control" placeholder="Titolo">
</div>
<br>
<div class="input-group">
<span class="input-group-addon">Admin</span>
<input id="admin" name="admin" type="text" class="form-control" value='{{$utente|lower}}'
placeholder='{{$utente}}'>
</div>
<br><br> <br> <br>
<div class="input-group">
<span class="input-group-addon">Immagine</span>
<input id="image" name="imageUP" accept="image/*" type="file" class="form-control"
placeholder="Immagine">
</div>
<br>
<div class="input-group">
<span class="input-group-addon">File</span>
<input id="image" name="fileUP" id="fileToUpload" type="file" class="form-control"
placeholder="FIle">
</div>
<br>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-font"></span></span>
<input id="content" name="content" type="text" class="form-control"
placeholder="Contenuto">
</div>
<br>
<button id="crea" type="submit" class="btn btn-warning">Crea</button>
</form>
Ajax 请求
$('#insert').submit(function (e) {
e.preventDefault();
var data = new FormData($(this)[0]);
$.ajax
({
url: 'uploads.php',
data: data,
type: 'post',
processData: false,
contentType: false,
success: function (response) {
response = parseInt(response);
switch (response) {
case -1: //errore generico
alert("errore");
break;
case 1:
alert("la creazione della news è andata a buon fine");
break;
}
close ajax call..
我的问题是: 脚本工作但效果不佳,我注意到如果我将文本放入“内容”输入并上传图片,查询不会插入数据。
在控制台中我有这个错误:
不允许加载本地资源:file:///C:/fakepath/xx.jpg
当我在 localhost 中工作时,我没有这个错误并且查询总是插入数据。现在我遇到了问题,我在真实服务器中。
有人知道怎么修吗? 我需要你的帮助
【问题讨论】:
-
我刚试了一下,效果很好,我又试了一次,还是不行。太奇怪了!
-
当脚本不起作用时,在控制台中我看到此请求没有可用的预览,并且没有 -1
-
upload 文件夹是否具有读写权限?
-
是的,我可以上传没有数据的图像(输入中没有文本)
-
如果我有文本输入脚本工作 7 次中有 2 次span>
标签: php jquery ajax file-upload