dqfaom241360
2017-03-23 06:52
浏览 61
已采纳

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

I am inserting file record in table with title of file, path, filename. The issue is when I enter title without the spaces the record gets inserted in the database but if I add spaces in title then the record is not getting inserted.

Only the file gets uploaded on the server if I add space in title and record dose not get inserted in the database.

If I do not add space in title it works well.

Main page

    <?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') . "
";
?> 
<!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>

File upload

    <?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."; 
    } 
}

?>

I am beginner in web development, can anyone help me with these please? Thank you..

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzongpeng6474 2017-03-23 08:31
    已采纳

    Enable PDO Exceptions with

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION`); 
    

    after your connect

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题