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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料