gpt4 book ai didi

php - 使用ajax和PHP上传图像

转载 作者:行者123 更新时间:2023-11-29 11:35:22 24 4
gpt4 key购买 nike

这是我的问题。我尝试上传图像,并借助此 URL 将图像存储在数据库中的相应文件夹和图像路径中。

http://www.lionblogger.com/how-to-upload-file-to-server-using-php-save-the-path-in-mysql/

当我尝试这样做时,我会得到正确的答案。在我尝试使用 AJAX 时,相同的代码无法正常工作。我不知道我犯了什么错误。下面是我的代码。

HTML 代码

<div class="input-group form-group">
<label> Upload Your Photo </label>
<input type="file" name="upload_photo" id="upload_photo">
</div>
<div class="">
<input type="submit" class="btn btn-success btn-lg " name="upload_files" id="upload_files" value="UPLOAD" >
</div>

AJAX代码

$("#upload_files").click(function(event){
event.preventDefault();
var upload_photo1 = $('#upload_photo').val();
var photo= upload_photo1.split('\\').pop().split('/').pop();
var datas="photo="+photo;
alert(datas);
if(photo==''){
sweetAlert({
title: "WARNING!!!",
text: "Please Upload All Corresponding Documents And Try Again !!!!",
type: "warning"
});
} else {
$.ajax({
type: "POST",
url: 'php/upload_files.php',
data:datas
}).done(function( data ) {
alert(data);
});
}
});

还有我的 PHP 文件上传文件.php

<?php
$fileExistsFlag = 0;
$fileName = $_POST['photo'];
var_dump($fileName);
$link = mysqli_connect("localhost","root","","spark") or die("Error ".mysqli_error($link));

$query = "SELECT filename FROM filedetails WHERE filename='$fileName'";
$result = $link->query($query) or die("Error : ".mysqli_error($link));
while($row = mysqli_fetch_array($result)) {
if($row['filename'] == $fileName) {
$fileExistsFlag = 1;
}
}

if($fileExistsFlag == 0)
{
$target = "files/";
$fileTarget = $target.$fileName;
$tempFileName = $_FILES["fileName"]["tmp_name"];
$fileDescription = $_POST['Description'];
$result = move_uploaded_file($tempFileName,$fileTarget);
$ext = end(explode('.', $fileName));
if ($_FILES["fileName"]["size"] > 2097152)
{
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
else if($ext != "jpg" && $ext != "png" && $ext != "jpeg"&& $ext != "gif" )
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
else
{
if($result) {
echo "Your file <html><b><i>".$fileName."</i></b></html> has been successfully uploaded";
$query = "INSERT INTO filedetails(filepath,filename,description) VALUES ('$fileTarget','$fileName','$fileDescription')";
$link->query($query) or die("Error : ".mysqli_error($link));
}
else {
echo "Sorry !!! There was an error in uploading your file";
}
}
mysqli_close($link);
}

else {
echo "File <html><b><i>".$fileName."</i></b></html> already exists in your folder. Please rename the file and try again.";
mysqli_close($link);
}
?>

我得到的错误是 enter image description here

请忍受我的疑问.. PHP 初学者。请帮助我解决这个问题。

最佳答案

您的 js 和 php 代码都存在严重问题。您根本没有上传文件。您刚刚上传了文件名。没有描述作为输入,但在您的 php 代码中,您有一个未处理的描述变量。您处理的文件名称错误。
尝试使用 FormData 上传文件。首先,使用带有 id 的 form 标签

<form id='myform'>
<div class="input-group form-group">
<label> Upload Your Photo </label>
<input type="file" name="upload_photo" id="upload_photo">
</div>
<div class="">
<input type="submit" class="btn btn-success btn-lg " name="upload_files" id="upload_files" value="UPLOAD" >
</div>
</form>

在js中,

    formData = new FormData($("#myForm")[0]);
formData.append("photo", photo);//your photo name

并在AJAX请求中写入

 data: formData,

在你的 php 文件中

<?php
$fileExistsFlag = 0;
$fileDescription ='No idea where this came from';
$fileName = $_POST['photo'];
$link = mysqli_connect("localhost","root","","spark") or die("Error ".mysqli_error($link));

$query = "SELECT filename FROM filedetails WHERE filename='$fileName'";
$result = $link->query($query) or die("Error : ".mysqli_error($link));
while($row = mysqli_fetch_array($result)) {
if($row['filename'] == $fileName) {
$fileExistsFlag = 1;
}
}

if($fileExistsFlag == 0)
{
$target = "files/";
$fileTarget = $target.$fileName;
$tempFileName = $_FILES["upload_photo"]["tmp_name"];
$result = move_uploaded_file($tempFileName,$fileTarget);
$ext = end(explode('.', $fileName));
if ($_FILES["upload_photo"]["size"] > 2097152)
{
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
else if($ext != "jpg" && $ext != "png" && $ext != "jpeg"&& $ext != "gif" )
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
else
{
if($result) {
echo "Your file <html><b><i>".$fileName."</i></b></html> has been successfully uploaded";
$query = "INSERT INTO filedetails(filepath,filename,description) VALUES ('$fileTarget','$fileName','$fileDescription')";
$link->query($query) or die("Error : ".mysqli_error($link));
}
else {
echo "Sorry !!! There was an error in uploading your file";
}
}
mysqli_close($link);
}

else {
echo "File <html><b><i>".$fileName."</i></b></html> already exists in your folder. Please rename the file and try again.";
mysqli_close($link);
}
?>

关于php - 使用ajax和PHP上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36693432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com