使用PHP上传文件并添加MySQL数据库的路径
Upload.php:使用PHP上传文件并添加MySQL数据库的路径,php,html,mysql,web,Php,Html,Mysql,Web,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=$_P
<?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.";
}
?>
($Filename=$_POST['Filename'];)
及
(已上载回显“the file”.basename($_FILES['uploadedfile']['Filename'])。“并且您的信息已添加到目录”;)
首先,您应该使用
print\r($\u文件)
进行调试,并查看其中包含的内容:
您的uploads.php
如下所示:
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("altabotanikk") or die(mysql_error()) ;
EDIT:由于这是一篇老文章,因此目前强烈建议在php中使用mysql_uu函数
// Connects to your Database
$link = mysqli_connect("localhost", "root", "", "");
这些已弃用,请使用以下内容
$sql = "INSERT INTO Table-Name (Column-Name)
VALUES ('$filename')" ;
要插入数据,请使用以下命令
$\u POST['Filename']不存在$_文件['uploadedfile']不存在$_文件['Filename']已存在。您可能希望回显$\u文件的值以查看数组结构:
echo“。打印($\u文件,true)。”如果我知道你到底是什么意思。。。那个片段去哪里了?谢谢,lopez.mikhaelin upload.php您应该使用$_文件来获取文件信息,而不是$_POST['filename']。如果要从表单中命名此文件,应将input type=text name=“文件名…您应该重新排序,首先将文件移动到目录中,然后将其保存到数据库中。mysql.*
函数很旧,很快将从PHP中完全删除。更新您的代码以使用PDO或mysqli,使其在将来继续工作。不,不尝试重命名该文件,我正在尝试上载该文件及其说明,其原始文件名根据$_FILES数组,原始文件名存储在$_FILES['filename']['name'].@bksi Yeah I do,现在我有5个错误,而不是一个错误:/Undefined index srcFile和move\u uploaded\u file()[]:copy()函数的第二个参数不能是目录+move\u uploaded\u file()[]:无法将'C:\WampServer\tmp\php7068.tmp'移动到'pics'
是的,我的错误,有人试图编辑我的帖子,但只返回了一个版本。试试这个。在你的形式中,你可以使用;i don’我没有未定义的索引srcFile,因为我从代码中删除了它。回答一个5年前提出的问题,并给出公认的答案,说代码不推荐使用,这不是很有用。
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
)
)
//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.";
}
?>
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')" ;