sql >> Database teknologi >  >> RDS >> Mysql

Brug af PHP til at uploade fil og tilføje stien til MySQL-databasen

Først skal du bruge print_r($_FILES) at fejlsøge og se, hvad den indeholder. :

dine uploads.php ville se sådan ud:

//This is the directory where images will be saved
$target = "pics/";
$target = $target . basename( $_FILES['Filename']['name']);

//This gets all the other information from the form
$Filename=basename( $_FILES['Filename']['name']);
$Description=$_POST['Description'];


//Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target)) {
    //Tells you if its all ok
    echo "The file ". basename( $_FILES['Filename']['name']). " has been uploaded, and your information has been added to the directory";
    // Connects to your Database
    mysql_connect("localhost", "root", "") or die(mysql_error()) ;
    mysql_select_db("altabotanikk") or die(mysql_error()) ;

    //Writes the information to the database
    mysql_query("INSERT INTO picture (Filename,Description)
    VALUES ('$Filename', '$Description')") ;
} else {
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
}



?>

EDIT: Da dette er et gammelt indlæg, anbefales det i øjeblikket stærkt at bruge enten mysqli eller pdo i stedet fungerer mysql_ i php



  1. Spring Boot Database initialisering MySQLException for Trigger

  2. MariaDB SESSION_USER() Forklaret

  3. Er det virkelig nødvendigt at oprette SQLite-tabeller, hver gang applikationen starter?

  4. SQL MELLEM-Smarte tips til at scanne efter en række værdier