使用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
        )

)

解决方法:

首先,您应该使用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.";
}



?>

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...