web-dev-qa-db-ja.com

PHPを使用してファイルをアップロードし、MySQLデータベースへのパスを追加する

Upload.php:

<?php

//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=$_POST['Filename'];
$Description=$_POST['Description'];
$pic=($_FILES['Filename']['name']);


// 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')") ;

//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['uploadedfile']['Filename']). " has been uploaded, and your information has been added to the directory";
} else {
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
}
?>

そして、これはフォームです(別のファイルにあります):

<form method="post" action="upload.php" enctype="multipart/form-data">
    <p>Photo:</p>
    <input type="file" name="Filename"> 
    <p>Description</p>
    <textarea rows="10" cols="35" name="Description"></textarea>
    <br/>
    <input TYPE="submit" name="upload" value="Add"/>
</form>

エラーは

 Undefined index: Filename on Line 17

($ Filename = $ _ POST ['Filename'];)

そして

Undefined index: uploadedfile on Line 35

(エコー "ファイル"。basename($ _FILES ['uploadedfile'] ['Filename'])。 "がアップロードされ、情報がディレクトリに追加されました";)

echo"<pre>".print_r($_FILES,true)."</pre>";

私に与えます:

Array
(
    [Filename] => Array
        (
            [name] => Laserkanon.jpg
            [type] => image/jpeg
            [tmp_name] => C:\WampServer\tmp\php11D4.tmp
            [error] => 0
            [size] => 41813
        )

)
6
The Last Melody

まず、print_r($_FILES)を使用してデバッグし、その内容を確認する必要があります。 :

きみの uploads.phpは次のようになります。

//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:これは古い投稿なので、現在は mysqli または pdo を使用することを強くお勧めします=代わりにPHPでmysql_関数

6
bksi
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("altabotanikk") or die(mysql_error()) ;

これらは非推奨です。以下を使用してください。

 // Connects to your Database
            $link = mysqli_connect("localhost", "root", "", "");

データを挿入するには、次を使用します

 $sql = "INSERT INTO  Table-Name (Column-Name)
VALUES ('$filename')" ;
0
Shahid Amin