使用PHP上传文件并添加MySQL数据库路径 [英] Using PHP to upload file and add the path to MySQL database

查看:32
本文介绍了使用PHP上传文件并添加MySQL数据库路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上传.php:

这是表格(在一个单独的文件中):

<p>照片:</p><input type="file" name="文件名"><p>描述</p><textarea rows="10" cols="35" name="Description"></textarea><br/><input TYPE="submit" name="upload" value="Add"/></表单>

错误是

 未定义索引:第 17 行的文件名

($Filename=$_POST['Filename'];)

未定义索引:第 35 行上载的文件

(echo "The file ".basename( $_FILES['uploadedfile']['Filename'])." 已上传,并且您的信息已添加到目录中";)

echo"

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

给我:

数组([文件名] =>大批([名称] =>Laserkanon.jpg[类型] =>图像/JPEG[tmp_name] =>C:\WampServer\tmp\php11D4.tmp[错误] =>0[尺寸] =>41813))

解决方案

首先你应该使用 print_r($_FILES) 来调试,看看它包含什么.:

您的 uploads.php 看起来像:

//这是保存图片的目录$target = "图片/";$target = $target .基本名称( $_FILES['文件名']['名称']);//这会从表单中获取所有其他信息$Filename=basename( $_FILES['Filename']['name']);$Description=$_POST['Description'];//将文件名写入服务器如果(move_uploaded_file($_FILES['文件名']['tmp_name'],$target)){//告诉你是否一切正常回声文件".基本名称($_FILES['文件名']['名称'])."已上传,您的信息已添加到目录";//连接到你的数据库mysql_connect("localhost", "root", "") 或 die(mysql_error()) ;mysql_select_db("altabotanikk") 或 die(mysql_error()) ;//将信息写入数据库mysql_query("插入图片(文件名,描述)值 ('$文件名', '$描述')") ;} 别的 {//如果不是,则给出错误echo "抱歉,上传您的文件时出现问题.";}?>

由于这是旧帖子,目前强烈建议使用 mysqlipdo 代替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.";
}
?>

And here is the form(in a separate 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>

The errors are

 Undefined index: Filename on Line 17

(the $Filename=$_POST['Filename'];)

and

Undefined index: uploadedfile on Line 35

(the echo "The file ". basename( $_FILES['uploadedfile']['Filename']). " has been uploaded, and your information has been added to the directory";)

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

gives me:

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

)

解决方案

First you should use print_r($_FILES) to debug, and see what it contains. :

your uploads.php would look like:

//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: Since this is old post, currently it is strongly recommended to use either mysqli or pdo instead mysql_ functions in php

这篇关于使用PHP上传文件并添加MySQL数据库路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆