使用PHP上传文件并将路径添加到MySQL数据库 [英] Using PHP to upload file and add the path to MySQL database
本文介绍了使用PHP上传文件并将路径添加到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Upload.php:
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
<$>($ Filename = $ _ POST [ 'Filename'];)
(the $Filename=$_POST['Filename'];)
和
Undefined index: uploadedfile on Line 35
(echoThe file。basename($ _FILES ['uploadedfile'] [ 'Filename'])。已被上传,并且您的信息已被添加到目录;)
(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
)
)
推荐答案
<首先,您应该使用 print_r($ _ FILES)
来进行调试,并查看它包含的内容。 :
First you should use print_r($_FILES)
to debug, and see what it contains. :
您的 uploads.php
需要:
your uploads.php
whould 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.";
}
?>
这篇关于使用PHP上传文件并将路径添加到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文