99ajohnson
99ajohnson

Reputation: 83

php upload images and save file name to mysql

I have this html form

<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"/>

and this is the 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);
?>

The covername is saved to the database and the file is uploaded to uploads/ - this works great. But the second upload isn't working at all, textname isn't saved to the db nor is it uploaded. What am i missing?

Upvotes: 1

Views: 2105

Answers (1)

Salim Ibrohimi
Salim Ibrohimi

Reputation: 1391

Just change this:

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

Upvotes: 1

Related Questions