我正在尝试上传文件,然后将文件名插入数据库。
以下是我的代码:
<?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.';
// }
}
?>
文件正在成功上传,但该名称未在数据库中更新。
请帮帮我。
答案 0 :(得分:2)
$n = $file['name']
;将返回任何内容,因为$ file未在任何地方声明。您可能想要使用:
$n = $_FILES['Filedata']['name'];
或其他任何声明你真的想要放入数据库的内容; - )
答案 1 :(得分:0)
你有$n = $file['name'];
应为$n = $targetFile
但首先 - 清理你的代码;)
我为你清理了它。检查这是否有效。
<?php
session_start();
include('/applications/MAMP/htdocs/connect.php');
mysqli_select_db("people");
if (!isset($_REQUEST['folder'])) $_REQUEST['folder'] = "";
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
$i = "UPDATE users SET filelocation = '$targetFile' WHERE sessionusername = '{$_SESSION['username']}'";
$q = mysqli_query($i);
//debug
echo "$targetFile<br/>
$tempFile<br/>
$i<br/>";
echo mysqli_error();
}
else {
echo "No file";
}
?>