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

标签 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."; 
}

最佳答案

改变

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

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

关于php - 通过 php 和 html 表单上传文件的代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25807370/

相关文章:

javascript - 悬停时更新 HTML5 Canvas 矩形?

MySQL 无法选择特定字段值?

php - 加载页面时未定义索引

mysql - Drupal 8 没有连接到 Docker 中的 Mysql

php - iconv UTF-8//忽略仍然产生 "illegal character"错误

php - 使用正则表达式替换搜索 MySQL 数据库

php - SQL将2列分成多行然后全部加起来

php - 如何插入多行,每行都有一个数组?

html - 表 nth-child 不工作

html - 导航菜单从容器中溢出