如何获取我上传的文件的文件名并将该文件名存储到sql中,

时间:2014-02-15 08:47:24

标签: php mysql sql file upload

我的问题是,如何获取我上传的文件的名称并将其存储到mysql中。这是我的代码:

define("UPLOAD_DIR", "../uploads/");

 if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"]['name'];

if ($myFile["error"] !== UPLOAD_ERR_OK) {
    echo "<p>An error occurred.</p>";
    exit;
}

// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
    $i++;
    $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}

// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
    UPLOAD_DIR . $name);
if (!$success) { 
    echo "<p>Unable to save file.</p>";
    exit;
}

// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);

这是我的SQL查询

$q= "INSERT INTO `discaction`(`posted_by`,`date_posted`,`date_happened`,`document`,`people_involved`,`reason`,`status`,`extra`) VALUES ('$posted_by','$date_posted','$_POST[monthreq]-$_POST[datereq]-$_POST[yearreq]','$name','$_POST[people_involved]','$_POST[reason]','$status','$extra')";

感谢您的回答。

0 个答案:

没有答案