我有两张想要上传到服务器的图片。如果两个文件都在html中设置,那么第一个查询应该执行,如果选择了image1而image2不是第二个查询shoud执行,如果未选择image1并且选择了inage2,那么如果没有选择图像则应该执行第三个查询在image1和image2中,然后执行查询4。
请注意,我还有三个文本框,无论是否选择了image1或image2,都会更新。
我遇到的问题是
当我在image1 nad image2中没有选择任何文件时,第一个代码通过插入图像1和图像2的情感值来执行,这是nto的意思。
是否有更清洁,更有效的方法来减少代码重复。
if (isset($_FILES['newsthumb']) && isset($_FILES['newsmain'])) {
$title = mysql_real_escape_string(trim($_POST['title']));
$body = mysql_real_escape_string(trim($_POST['body']));
$mainimage_title = mysql_real_escape_string(trim($_POST['mainimage_title']));
$thumbimage = $_FILES['newsthumb'];
$mainimage = $_FILES['newsmain'];
$unique = time();
$thumbname = strtolower($thumbimage['name']);
$mainmane = strtolower($mainimage['name']);
$thumbname = preg_replace("/[^A-Z0-9._-]/i", "_", $thumbname);
$mainmane = preg_replace("/[^A-Z0-9._-]/i", "_", $mainmane);
$thumbname = mysql_real_escape_string($thumbname);
$mainmane = mysql_real_escape_string($mainmane);
$uploaddir = "images/newsimage/";
$thumbsuccess = move_uploaded_file($thumbimage["tmp_name"], $uploaddir.$thumbname);
$mainsuccess = move_uploaded_file($mainimage["tmp_name"], $uploaddir.$mainmane);
$newsarticle = "UPDATE news
SET title = '$title',
body = '$body',
mainimage_title = '$mainimage_title' ,
thumbnail = '$thumbname',
mainimage = '$mainmane',
editdate = NOW()
WHERE id = '$article_id'";
mysql_query($newsarticle) or die (mysql_error());
}
if (!isset($_FILES['newsthumb']) && isset($_FILES['newsmain'])) {
$title = mysql_real_escape_string(trim($_POST['title']));
$body = mysql_real_escape_string(trim($_POST['body']));
$mainimage_title = mysql_real_escape_string(trim($_POST['mainimage_title']));
// Commented out since it is not defined anywhere in your original posted code.
// You will have to implement that in.
$mainimage = $_FILES['newsmain'];
$unique = time();
$mainmane = strtolower($mainimage['name']);
$mainmane = preg_replace("/[^A-Z0-9._-]/i", "_", $mainmane);
$mainmane = mysql_real_escape_string($mainmane);
$uploaddir = "images/newsimage/";
$mainsuccess = move_uploaded_file($mainimage["tmp_name"], $uploaddir.$mainmane);
$newsarticle = "UPDATE news
SET title = '$title',
body = '$body',
mainimage_title = '$mainimage_title' ,
mainimage = '$mainmane',
editdate = NOW()
WHERE id = '$article_id'";
mysql_query($newsarticle) or die (mysql_error());
}
if (isset($_FILES['newsthumb']) && (!isset($_FILES['newsmain']))) {
$title = mysql_real_escape_string(trim($_POST['title']));
$body = mysql_real_escape_string(trim($_POST['body']));
$mainimage_title = mysql_real_escape_string(trim($_POST['mainimage_title']));
// Commented out since it is not defined anywhere in your original posted code.
// You will have to implement that in.
$thumbimage = $_FILES['newsthumb'];
$unique = time();
$thumbname = strtolower($thumbimage['name']);
$thumbname = preg_replace("/[^A-Z0-9._-]/i", "_", $thumbname);
$thumbname = mysql_real_escape_string($thumbname);
$uploaddir = "images/newsimage/";
$thumbsuccess = move_uploaded_file($thumbimage["tmp_name"], $uploaddir.$thumbname);
$newsarticle = "UPDATE news
SET title = '$title',
body = '$body',
mainimage_title = '$mainimage_title' ,
thumbnail = '$thumbname',
editdate = NOW()
WHERE id = '$article_id'";
mysql_query($newsarticle) or die (mysql_error());
}
if (!isset($_FILES['newsthumb']) && (!isset($_FILES['newsmain']))) {
$title = mysql_real_escape_string(trim($_POST['title']));
$body = mysql_real_escape_string(trim($_POST['body']));
$mainimage_title = mysql_real_escape_string(trim($_POST['mainimage_title']));
$newsarticle = "UPDATE news
SET title = '$title',
body = '$body',
mainimage_title = '$mainimage_title' ,
editdate = NOW()
WHERE id = '$article_id'";
mysql_query($newsarticle) or die (mysql_error());
}
答案 0 :(得分:1)
也许通过创建一个函数来获取参数和他的东西中的文件。没有重复会有所帮助。
用于检查选择的文件或用于:
if ($_FILES["file"]["error"] > 0)
更多解释:
function uploadASingleFile($file){
// Do your stuff here for the upload (moving the file, sql request etc.)
}
然后,检查文件是否被选中(使用[“错误”])并调用函数
if($_FILES["file1"]["error"] == 0){
uploadASingleFile($_FILES["file1"]); // Obviously, handle errors...
}
if($_FILES["file2"]["error"] == 0){
uploadASingleFile($_FILES["file2"]); // Obviously, handle errors...
}
代码不那么重复和清晰。我回答了你的问题吗?