如何在服务器上插入文件名,标题和上传文件

时间:2017-03-23 06:52:44

标签: php mysql file upload server

我在文件中插入文件记录,文件名,路径,文件名。 问题是当我输入没有空格的标题时,记录被插入到数据库中,但如果我在标题中添加空格,则记录不会被插入。

如果我在标题中添加空格并且记录剂量未插入数据库,则只有文件上传到服务器上。

如果我不在标题中添加空格,那么效果很好。

主页

    <?php 
ini_set('display_errors', 1); 
error_reporting(1); 
ini_set('error_reporting', E_ALL); 

session_start(); 


echo 'post_max_size = ' . ini_get('post_max_size') . "\n";
?> 
<!DOCTYPE html> 
<html> 
<head> </head> 
<body> 
<title>File upload</title>

<form action="fileUpload.php" method="post" enctype="multipart/form-data"> 
 Select image to upload:
<br><br>
<input name = "file" type="file" id="fileToUpload"><br><br> 
Enter title : <input name="title" id="title" type="text"><br><br>

Select question type : <br><br> 

<?php 
    if (isset($_SESSION['type']))
{
$type = $_SESSION["type"]; ?>

    <div id="types"> 

SSgt <input name="type" type="radio" id="t2" value="SSgt" <?=($type==1?"checked":"");?>> 

TSgt <input name="type" type="radio" id="t1" value="TSgt" <?=($type==2?"checked":"");?>> 

MSgt <input name="type" type="radio" id="t3" value="MSgt" <?=($type==3?"checked":"");?>> 
</div> <br><br>
<?php
}
 else {
    ?>

    <div id="types"> 

SSgt <input name="type" type="radio" id="t2" value="SSgt"> 

TSgt <input name="type" type="radio" id="t1" value="TSgt"> 

MSgt <input name="type" type="radio" id="t3" value="MSgt"> 
</div> <br><br>

<?php 
}
?>

<input type="submit" value = "Upload File"> 
</form> 
</body> 
</html>

文件上传

    <?php      
ini_set('display_errors', 1);
error_reporting(1); 
ini_set('error_reporting', E_ALL);

session_start();
$file_result = "";

if($_FILES["file"]["error"] > 0)
{
    $file_result .= "No file uploaded or invalid file.";
    $file_result .= "Error code : " . $_FILES["file"]["error"] . "<br>" ;

}
else{
    $target_dir = "files/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
    $file_result .=
        "Upload " . $_FILES["file"]["name"] . "<br>" .
        "type " . $_FILES["file"]["type"] . "<br>" .
        "temp file " . $_FILES["file"]["tmp_name"] . "<br>";


    if(move_uploaded_file($_FILES["file"]["tmp_name"],$target_file)){

        echo "The file ". basename( $_FILES['file']['name']). " has been uploaded, and your information has been added to the directory"; 

        $type = $_POST['type'];
        $title = $_POST['title']; 


if(strcmp($type,'SSgt') == 0)
{
    $queType = 1;
}
elseif(strcmp($type,'TSgt') == 0)
{
    $queType = 2;
}
elseif(strcmp($type,'MSgt') == 0)
{
    $queType = 3;
}
        $dbh = new PDO('mysql:host=;dbname=air','airman', 'ai');

        //Writes the information to the database 
        $stmt = $dbh->prepare("INSERT INTO files (file,type,title,path) VALUES (?,?,?,?)");
        $stmt->execute(array($_FILES['file']['name'],$queType,$title,$target_file));

        if ($dbh->lastInsertId())
        {
            //echo 'File inserted   .';

            $_SESSION["type"] = $queType;

            echo '<a href="MainPage.php"><br> Upload another file.</a>';
        }
        else
        {
            echo($title);

            echo 'File could not insert.';
        }

    } 
    else { 

         //Gives and error if its not 
            echo "Sorry, there was a problem uploading your file."; 
    } 
}

?>

我是网络开发的初学者,有人可以帮我这些吗?谢谢..

1 个答案:

答案 0 :(得分:1)

使用

启用PDO例外
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION`); 
连接后