我有一个表单,您可以在其中上传图像,这是可选的。现在使用此脚本将上传/时间戳写入我的数据库。但如果没有上传文件,我需要用占位符替换它。这是代码:
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 1024000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$path_parts = pathinfo($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $path_parts['filename'].'_'.time().'.'.$path_parts['extension']);
}
}
else
{
echo "Invalid file";
}
和:
$name1 = $_REQUEST['name1'];
$email1 = $_REQUEST['email1'];
$beschreibung1 = $_REQUEST['beschreibung1'] ;
$latitude = $_REQUEST['latitude'] ;
$longitude = $_REQUEST['longitude'] ;
$file = $_REQUEST['file'] ;
$pic = ("upload/" . $path_parts['filename'].'_'.time().'.'.$path_parts['extension']);
include 'bikemap_db_open.php';
$sql = "INSERT INTO input2 (f_name, f_e_mail, f_text, f_adr, f_dat, f_geom, f_foto)";
$sql = $sql . " VALUES ('" . $name1 . "','" . $email1 . "','" . $beschreibung1 .
"','" . $longitude . " " . $latitude . "', '" . date("d-m-Y") . "', ST_GeomFromText('POINT(" . $longitude . " " . $latitude . ")', 4326),'" . $pic . "')";
$result = pg_query($sql) or die('Query failed: ' . pg_last_error());
include 'bikemap_db_close.php';
header( "Location: /danke.html" );exit;
最佳答案
在第一个代码块中
else
{
echo "Invalid file";
$fileupload = false;
}
在第二个代码块中
if ($fileupload == true){
$pic = ("upload/" . $path_parts['filename'].'_'.time().'.'.$path_parts['extension']);
}
else
{
$pic = 'placeholder.gif'; // you will need to specify the full path
}
关于php - 如果没有文件上传,数据库中的文件id应该是: placeholder. gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20268301/