我正在尝试上传文件,然后将文件名插入数据库。
下面是我的代码:
<?php
//connecting to database
session_start();
include('/applications/MAMP/htdocs/connect.php');
$u = $_SESSION['username'];
if (!empty($_FILES)) {
$file = $_FILES['Filedata'];
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
// $fileTypes = str_replace('*.','',$_REQUEST['fileext']);
// $fileTypes = str_replace(';','|',$fileTypes);
// $typesArray = split('\|',$fileTypes);
// $fileParts = pathinfo($_FILES['Filedata']['name']);
// if (in_array($fileParts['extension'],$typesArray)) {
// Uncomment the following line if you want to make the directory if it doesn't exist
// mkdir(str_replace('//','/',$targetPath), 0755, true);
$n = $file['name'];
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
mysqli_select_db($connect,"people");
$i = "update users set filelocation = '$n' where sessionusername = '$u'";
$q = mysqli_query($connect,$i);
// } else {
// echo 'Invalid file type.';
// }
}
?>
文件正在上传成功,但数据库中的名称未更新。
请帮帮我。
最佳答案
$n = $file['name']
;不会返回任何内容,因为 $file 没有在任何地方声明。你可能想使用:
$n = $_FILES['Filedata']['name'];
或任何其他声明你真的想放入你的数据库 ;-)
关于php - 上传文件后更新数据库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5116009/