我有一个可用的HTML表单,允许人们附加一些图像并将其激活。我想将表单的内容存储在数据库中(图像除外),并将图像上传到托管站点上的文件夹。
图像名称将放在它们自己的文件夹中(使用纪元时间作为文件夹名称),并将它们的名称插入到数据库中以及以后检索的时间戳。
代码执行时没有错误,但没有任何上传,也没有任何东西进入数据库。现在我使用的是简化的代码而没有做太多的检查(在那里减少一些松弛),因为这只是概念的证明。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('HOST', 'xxx');
define('DB', 'xxx');
define('USER', 'xxx');
define('PASS', 'xxx');
/*not being used for now, will be later in production
function mts_sanitize($data) {
//clean the input
$string = mysqli_real_escape_string($data);
$string = htmlentities($data);
return $string;
}*/
function insertmessage($msg, $filesmts, $intime) {
//get the message and file names nad insert them into the db
$fileloc = implode(":", $filesmts);
$conn = new mysqli(HOST, USER, PASS, DB);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO submissions ('time', 'message', 'files') VALUES ($intime, $msg, $fileloc)";
if ($conn->query($sql) === TRUE) {
uploadimg($files, $filenames, $intime);
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
function uploadimg($files, $filenames, $intime) {
//upload the actual images
$target_dir = "forms/uploads/".$intime."/";
foreach($files as $k=>$v) {
$target_file = $target_dir.$filesnames[$k];
move_uploaded_file($v, $target_file);
}
}
这是我第一次尝试通过表单上传文件。
编辑:
让我们假设我的形式没有被破坏,它实际上是有效的,它是......我只是试图看看这个特定的函数集是否有任何错误。他们并没有误导我,而是没有做好自己的工作,而我只是很难搞清楚这一点。
我的消息是一个格式化的字符串,当我从数据库中取出它时可读取(转义字符)。文件名的派生方式如下:
// Temporary paths of selected files
$file1 = $_FILES['image1']['tmp_name'];
$file2 = $_FILES['file2']['tmp_name'];
$file3 = $_FILES['file3']['tmp_name'];
$file4 = $_FILES['file4']['tmp_name'];
$file5 = $_FILES['file5']['tmp_name'];
// File names of selected files
$filename1 = $_FILES['image1']['name'];
$filename2 = $_FILES['file2']['name'];
$filename3 = $_FILES['file3']['name'];
$filename4 = $_FILES['file4']['name'];
$filename5 = $_FILES['file5']['name'];]
$files = array($file1, $file2, $file3, $file4, $file5);
$filenames = array($filename1, $filename2, $filename3, $filename4, $filename5);
然后我在消息发送之前调用该函数,如下所示:
//validation code above
require_once('dbinsert.php'); //My file that is pasted above
$intime = time();
insertmessage($msgmts, $filenames, $intime);
//sending code below. Again this works with out error and sends the email.
答案 0 :(得分:1)
$files
数组永远不会进入uploadimg
函数。在insertmessage
内部调用此函数,该函数没有传递给它$files