微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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.";
}



?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐