gpt4 book ai didi

php - 使用php和mysql上传图片

转载 作者:行者123 更新时间:2023-11-29 11:02:06 25 4
gpt4 key购买 nike

我正在构建一个上传页面,允许我上传图片,记录将存储在 MySQL 中。现在,我可以保存记录。但是,我在将图片存储在实际文件中时遇到问题,并且图片未存储。

代码如下:

  <?php
include "config.php";

if (isset($_FILES['file'])) {
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp = $_FILES['file']['tmp_name'];
$file = 'uploads'. $_FILES['file']['name'];
$upload = move_uploaded_file($tmp, $file);
if ($upload) {
$add = $db->prepare("insert into upload values('',?)");
$add->bindParam(1, $name);
if ($add->execute()) {
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Success!</strong> File upload successful to database.
</div>
<?php
} else {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Failed!</strong> File upload unsuccessful to database.
</div>
<?php
}
} else {
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Sorry!</strong> File upload unsuccessful .
</div>
<?php
}
}
?>

这是我的文件: enter image description here

在我回显 $file 和 $tmp 后: enter image description here

最佳答案

改变这个

$file = 'uploads'. $_FILES['file']['name'];

到此

$file = '../uploads/'. $_FILES['file']['name'];

您的实际代码会在您的代码所在的 uploadTesting 文件夹中搜索 upload 文件夹。

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

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