我在这之后搜索过但找不到任何清楚的东西。 我有一个包含2个输入文件字段的表单,我需要插入所有数据。 我想要多文件上传,我需要将每个输入文件分开。 我喜欢上面的代码导致它重命名每个文件。 我试过这个
`$host="hst";
$databasename="nme";
$user="usr";
$pass="pwd";
/**********MYSQL Settings****************/
$conn=mysql_connect($host,$user,$pass);
if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
die('Not connected : ' . mysql_error());
}
$name = $_POST["name"] ;
//email:
$email = $_POST["email"] ;
//description:
$description = $_POST["description"] ;
//partie:
$partie = $_POST["partie"] ;
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
if (!empty($_FILES["image"]["name"])) {
$file_name=$_FILES["image"]["name"];
$temp_name=$_FILES["image"]["tmp_name"];
$imgtype=$_FILES["image"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = "images/assets/syltattoo/Emails/".$imagename;
if (!empty($_FILES["image2"]["name"])) {
$file_name2=$_FILES["image2"]["name"];
$temp_name2=$_FILES["image2"]["tmp_name2"];
$imgtype2=$_FILES["image2"]["type2"];
$ext= GetImageExtension($imgtype);
$imagename2=date("d-m-Y")."-".time().$ext;
$target_path2 = "images/assets/syltattoo/Emails/".$imagename2;
if(move_uploaded_file($temp_name, $target_path)) {
$query_upload="INSERT INTO `modx_demandes`(image, image2, name, email, description, partie) VALUES
('".$target_path."','".$target_path2."','$name', '$email', '$description', '$partie')";
mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());
}else{
exit("Error While uploading image on the server");
}
}}`
以下是表格:
<form action="demandes-de-tatouage.html" method="post" enctype="multipart/form-data">
<div id="name"><label for="name">Nom:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="name" value="" /></div>
<div id="name"><label for="name">Email:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="email" value="" /></div>
<div id="description"><label for="description">Description:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="description" value="" /></div>
<div id="name"><label for="name">Partie du corps:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="partie" value="" /></div>
<div>
<label for="image">Envoyez vos images</label></div>
<input id="image" name="image" type="file" value="" maxlength="100000" />
<input id="image2" name="image2" type="file" value="" maxlength="100000" />
<input class="btn btn-primary" type="submit" value="Envoyer" /></form>
我尝试集成第二个image2字段,但它为image1和image2保存了相同的数据库结果。有人可以帮助我使用这个脚本制作一些我可以使用多个输入字段图像的东西吗?
答案 0 :(得分:0)
这是因为它的文件名与日期和时间相同... put
$imagename2=date("d-m-Y")."-".time() . "-2" .$ext;
表示第二个文件名
您还需要移动第二个文件
if(move_uploaded_file($temp_name, $target_path) && move_uploaded_file($temp_name2, $target_path2)) {
答案 1 :(得分:0)
您在脚本中输入了一些拼写错误(您从$imgtype
获得了相同的扩展名),并且在将它们插入数据库时您没有使用原始文件名。此外,你没有在任何地方保存第二个文件。这段代码适合你吗?
<?php
$host = "hst";
$databasename = "nme";
$user = "usr";
$pass = "pwd";
/**********MYSQL Settings****************/
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
} else {
die('Not connected : ' . mysql_error());
}
$name = $_POST["name"];
//email:
$email = $_POST["email"];
//description:
$description = $_POST["description"];
//partie:
$partie = $_POST["partie"];
function GetImageExtension($imagetype)
{
if (empty($imagetype)) {
return false;
}
switch ($imagetype) {
case 'image/bmp':
return '.bmp';
case 'image/gif':
return '.gif';
case 'image/jpeg':
return '.jpg';
case 'image/png':
return '.png';
default:
return false;
}
}
if (!empty($_FILES["image"]["name"])) {
$file_name = $_FILES["image"]["name"];
$temp_name = $_FILES["image"]["tmp_name"];
$imgtype = $_FILES["image"]["type"];
$ext = GetImageExtension($imgtype);
$imagename = $file_name . "-" . date("d-m-Y") . "-" . time() . $ext;
$target_path = "images/assets/syltattoo/Emails/" . $imagename;
if (!empty($_FILES["image2"]["name"])) {
$file_name2 = $_FILES["image2"]["name"];
$temp_name2 = $_FILES["image2"]["tmp_name"];
$imgtype2 = $_FILES["image2"]["type"];
$ext2 = GetImageExtension($imgtype2);
$imagename2 = $file_name2 . "-" . date("d-m-Y") . "-" . time() . $ext2;
$target_path2 = "images/assets/syltattoo/Emails/" . $imagename2;
if (move_uploaded_file($temp_name, $target_path) && move_uploaded_file($temp_name2, $target_path2)) {
$query_upload = "INSERT INTO `modx_demandes`(image, image2, name, email, description, partie) VALUES
('" . $target_path . "','" . $target_path2 . "','$name', '$email', '$description', '$partie')";
mysql_query(
$query_upload
) or die("error in $query_upload == ----> " . mysql_error());
} else {
exit("Error While uploading image on the server");
}
}
}
?>
这是一些重构的代码。即使用户没有发送任何图像,此代码也应该有效。
<?php
$host = "hst";
$databasename = "nme";
$user = "usr";
$pass = "pwd";
/**********MYSQL Settings****************/
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
} else {
die('Not connected : ' . mysql_error());
}
$name = $_POST["name"];
//email:
$email = $_POST["email"];
//description:
$description = $_POST["description"];
//partie:
$partie = $_POST["partie"];
function GetImageExtension($imagetype)
{
if (empty($imagetype)) {
return false;
}
switch ($imagetype) {
case 'image/bmp':
return '.bmp';
case 'image/gif':
return '.gif';
case 'image/jpeg':
return '.jpg';
case 'image/png':
return '.png';
default:
return false;
}
}
function uploadImage($image) {
if(!empty($image["name"])) {
$file_name = $image["name"];
$temp_name = $image["tmp_name"];
$imgtype = $image["type"];
$ext = GetImageExtension($imgtype);
$imagename = $file_name . "-" . date("d-m-Y") . "-" . time() . $ext;
$target_path = "images/assets/syltattoo/Emails/" . $imagename;
if (move_uploaded_file($temp_name, $target_path)) {
return $target_path;
}
exit("Error While uploading image on the server");
}
return null;
}
$target_path = uploadImage($_FILES["image"]);
$target_path2 = uploadImage($_FILES["image2"]);
$query_upload = "INSERT INTO `modx_demandes`(image, image2, name, email, description, partie) VALUES ('" . $target_path . "','" . $target_path2 . "','$name', '$email', '$description', '$partie')";
mysql_query($query_upload) or die("error in $query_upload == ----> " . mysql_error());
?>