代码之家  ›  专栏  ›  技术社区  ›  99ajohnson

php上传图像并将文件名保存到mysql

  •  1
  • 99ajohnson  · 技术社区  · 8 年前

    我有这个html表单

    <form action="insert.php" method="post" enctype="multipart/form-data">>
    <p>
        <label for="covername">Cover Artwork:</label>
        <input type="file" name="file"/>
    </p>
    <p>
        <label for="textname">Text Artwork:</label>
        <input type="file" name="textname"/><br><br>
    </p>
    
    <input type="submit" name="submit" value="Upload"/>
    

    这是插页。php

    //Upload cover artwork
    $name= $_FILES['file']['name'];
    $tmp_name= $_FILES['file']['tmp_name'];
    $submitbutton= $_POST['submit'];
    $position= strpos($name, "."); 
    $fileextension= substr($name, $position + 1);
    $fileextension= strtolower($fileextension);
    
    if (isset($name)) {
    
    $path= 'uploads/';
    
    if (!empty($name)){
    if (move_uploaded_file($tmp_name, $path.$name)) {
    echo 'Uploaded!';
    
    }
    }
    }
    
    
    //Upload text artwork
    $textname= $_FILES['file']['textname'];
    $tmp_textname= $_FILES['file']['tmp_textname'];
    $textsubmitbutton= $_POST['submit'];
    $textposition= strpos($textname, "."); 
    $textfileextension= substr($textname, $textposition + 1);
    $textfileextension= strtolower($textfileextension);
    
    if (isset($textname)) {
    
    $textpath= 'uploads/';
    
    if (!empty($textname)){
    if (move_uploaded_file($tmp_textname, $textpath.$textname)) {
    echo 'Uploaded!';
    
    }
    }
    }
    
    // attempt insert query execution
    $sql = "INSERT INTO table (covername, textname) VALUES ('$name', '$textname')";
    if(mysqli_query($link, $sql)){
        echo "Records added successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
    
    // close connection
    mysqli_close($link);
    ?>
    

    covername被保存到数据库中,文件被上传到uploads/-这很好用。但是第二次上传根本不起作用,textname没有保存到db,也没有上传。我错过了什么?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Salim Ibrohimi    8 年前

    只需更改以下内容:

    //Upload text artwork
    $textname= $_FILES['textname']['name'];
    $tmp_textname= $_FILES['textname']['tmp_name'];
    $textsubmitbutton= $_POST['submit'];
    $textposition= strpos($textname, "."); 
    $textfileextension= substr($textname, $textposition + 1);
    $textfileextension= strtolower($textfileextension);
    
    if (isset($textname)) {
    
    $textpath= 'uploads/';
    
    if (!empty($textname)){
    if (move_uploaded_file($tmp_textname, $textpath.$textname)) {
    echo 'Uploaded!';