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

时间:2018-02-21 10:39:28

标签: php mysql file-upload

我有这个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"/>

这是insert.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保存到数据库,文件上传到上传/ - 这很好用。但是第二次上传根本不起作用,textname没有保存到db,也没有上传。我错过了什么?

1 个答案:

答案 0 :(得分:1)

只需改变一下:

//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!';