通过php和html表单上传文件的代码出错

时间:2014-09-12 11:34:02

标签: php html mysql file-upload

我遇到以下代码错误,导致文件无法上传到服务器。有人可以看看这两个文件,并告诉我哪里弄错了,我知道我可能已经很厚,但任何帮助都会非常感激。

这些文件位于一个小型内部Intranet上,所以我并不关心将数据库连接和密码放入文件的事实。

正如我所说,任何帮助都会受到高度赞赏,因为我只收到最后的抱歉错误,并且没有信息传递给MySQL数据库。我可以让表单和代码在没有上传但没有上传的情况下正常工作。 我已经检查了上传目录和访问它的权限,一切正常。

html表单:

<form enctype="multipart/form-data" action="add.php" method="POST"> 
Model:<br /> <input type="text" name="model"><br> 
Size:<br /> <input type="text" name="size"><br> 
Total width:<br /> <input type="text" name="tot_width"><br>
Internal width:<br /> <input type="text" name="int_width"><br>
Total height:<br /> <input type="text" name="tot_height"><br>
Frame height:<br /> <input type="text" name="fra_height"><br>
Total depth:<br /> <input type="text" name="tot_depth"><br>
Seat height:<br /> <input type="text" name="sea_height"><br>
Seat depth:<br /> <input type="text" name="sea_depth"><br> 
Arm height:<br /> <input type="text" name="arm_height"><br>
Std leg height:<br /> <input type="text" name="std_leg_height"><br>
Image:<br /> <input type="file" name="image"><br>
<input type="submit" value="Add"> 
</form>

PHP:

//This is the directory where images will be saved 
$target = "/images/product"; 
$target = $target . basename( $_FILES['image']['name']); 

//Get all the other information from the form 
$model=$_POST['model'];
$size=$_POST['size'];
$totWidth=$_POST['tot_width'];
$intWidth=$_POST['int_width'];
$totHeight=$_POST['tot_height'];
$fraHeight=$_POST['fra_height'];
$totDepth=$_POST['tot_depth'];  
$seaHeight=$_POST['sea_height'];
$seaDepth=$_POST['sea_depth'];
$armHeight=$_POST['arm_height'];
$stdLegHeight=$_POST['std_leg_height']; 
$pic=($_FILES['image']['name']);

// Connect to Database 
mysql_connect("localhost", "databaseUser", "databasePassword") or die(mysql_error()) ; 
mysql_select_db("databaseName") or die(mysql_error()) ; 

//Write information to the database 
mysql_query("INSERT INTO `prod_dims` VALUES '$model', '$size', '$totWidth', '$intWidth', '$totHeight', '$fraHeight', '$totDepth', '$seaHeight', '$seaDepth', '$armHeight', '$stdLegHeight', '$pic'") ; 

//Put the image on the server 
if(move_uploaded_file($_FILES['image']['name'], $target)) 
{ 

//Is the upload ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and     your information has been added to the directory"; 
} 
else { 

//Give an error if its not 
echo "Sorry, there was a problem uploading your file."; 
}

1 个答案:

答案 0 :(得分:0)

变化

if(move_uploaded_file($_FILES['image']['name'], $target))

if(move_uploaded_file($_FILES["image"]["tmp_name"], $target))