php 使用PHP上传文件并添加MySQL数据库路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17153624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using PHP to upload file and add the path to MySQL database
提问by The Last Melody
Upload.php:
上传.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'];)
($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 "The file ".basename( $_FILES['uploadedfile']['Filename'])."已经上传,你的信息已经添加到目录中";)
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
)
)
采纳答案by bksi
First you should use print_r($_FILES)
to debug, and see what it contains. :
首先你应该使用print_r($_FILES)
调试,看看它包含什么。:
your uploads.php
would look like:
你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:Since this is old post, currently it is strongly recommended to use either mysqlior pdoinstead mysql_ functions in php
回答by Shahid Amin
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("altabotanikk") or die(mysql_error()) ;
These are deprecated use the following..
这些已弃用,请使用以下内容..
// Connects to your Database
$link = mysqli_connect("localhost", "root", "", "");
and to insert data use the following
并插入数据使用以下
$sql = "INSERT INTO Table-Name (Column-Name)
VALUES ('$filename')" ;