我已创建文件index.php用于上传图像,上传后它将存储在数据库中并显示在主页中。现在它工作正常。
现在我添加了更多字段,如firstname,lastname和其他用户必填字段。
现在显示问题上传图片。
我可以知道,修复它的确切方法是什么?
任何帮助都将不胜感激。
谢谢!!。
这是index.php:
<html>
<head>
<title>upload images to database</title>
</head>
<body>
<h1>Register Form</h1>
<form action="index.php" method = "POST" enctype = "multipart/form-data">
Upload Photo:<input type= "file" name= "image"><br />
<input type= "submit" value= "upload">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//connect to database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("image") or die(mysql_error());
$file = '';
$file= $_FILES['image'] ['tmp_name'];
if(!isset($file))
{
echo "please select an image";
}
else
{
$image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$image_name = mysql_real_escape_string($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size ==FALSE)
{
echo "That's not an image";
}
else
{
if(!$insert = mysql_query("INSERT INTO upload VALUES('','$image_name','$image')"))
{
echo "Problem uploading image";
}
else{
$lastid = mysql_insert_id();
echo "Image upload.<p/>Your Image</p><img src=get.php?id=$lastid>";
}
}
}
}
?>
</body>
</html>
此代码在上传图片时效果很好,但是当我添加时,这些代码行,我上传图片时出现问题
First Name: <input type = "text" name= "fname"><br />
Last Name:<input type = "text" name= "lname"><br />
Password :<input type = "text" name = "password"><br />
Retype-password: <input type = "text" name = "rpassword"><br />
Email:<input type = "text" name = "email"><br />
Phone Num: <input type = "text" name = "phone"><br />
Address: <input type ="text" name = "address"><br />
任何人都可以帮助我,还需要添加哪些代码?
答案 0 :(得分:0)
你的问题非常含糊不清,但我冒这里的一些假设
i added more fields like firstname,lastname and other user required fields.
如果你这意味着你在名为upload
的数据库表中添加了更多字段,那么你的错误就在下面的SQL中:
...mysql_query("INSERT INTO upload VALUES('','$image_name','$image')"))...
这里没有提到字段名称,如果要在所有字段中插入值,则可以正常使用。如果未在所有表字段中插入值,则需要指定要插入这些值的确切字段。类似的东西:
...mysql_query("INSERT INTO upload (field1,field2,field3)
VALUES('','$image_name','$image')"))...
答案 1 :(得分:0)
要查看您是否怀疑由于新添加的html代码导致上传失败 删除数据库部分 数据库部分不是必需的,只会使故障排除变得复杂。
uploadFile
。 bool move_uploaded_file ( string $filename , string $destination )
设置为应用路径内的文件夹
$destination ="images/".$_FILES['image']['name'];
或设置为绝对文件夹
$destination ="/home/..../http/pre/rid/25/132596/htdocs/myAppDir/images/"
.$_FILES['image']['name'];
使用phpinfo();
$file= $_FILES['image']['tmp_name'];
if ( move_uploaded_file ($file , $destination )) {
$upOK = TRUE;
} else {
$upOK = FALSE;
echo "File could not be moved : ".$destination;
}
phpinfo()函数
常见陷阱
MAX_FILE_SIZE项目无法指定的文件大小大于php.ini文件中upload_max_filesize中设置的文件大小。默认值为2兆字节。
如果启用了内存限制,则可能需要更大的memory_limit。确保将memory_limit设置得足够大。
如果max_execution_time设置得太小,则值可能会超出脚本执行。确保将max_execution_time设置得足够大。
测试错误
switch ($_FILES['image']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
echo "No file sent.";
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
echo "Exceeded filesize limit.";
throw new RuntimeException('Exceeded filesize limit.');
default:
echo "Unknown errors";
throw new RuntimeException('Unknown errors.');
}
// You should also check filesize here.
if ($_FILES['image']['size'] > 1000000) {
echo "Exceeded filesize limit.";
throw new RuntimeException('Exceeded filesize limit.');
}
如何设置发布值
[...]
<?php
if (isset($_POST["fname"])) {$fname=$_POST["fname"];} else {$fname = "";}
if (isset($_POST["lname"])) {$lname=$_POST["lname"];} else {$lname = "";}
// et cetera
?>
<form action="index.php" method = "POST" enctype = "multipart/form-data">
First Name: <input type = "text" name= "fname" value="<?php echo $fname;?>"><br />
Last Name : <input type = "text" name= "lname" value="<?php echo $lname;?>"><br />
// et cetera
[...]