Php内容插入页面不工作
我一直在尝试用php制作一个内容插入页面,下面是我的代码Php内容插入页面不工作,php,file-upload,boolean,content-management-system,Php,File Upload,Boolean,Content Management System,我一直在尝试用php制作一个内容插入页面,下面是我的代码 <?php // Initialize variables to null. $title =""; // Sender Name $author =''; // Sender's email ID $date =date('d-m-y'); // Subject of mail $desc="";//meta description $keywords="";//meta keywords $content =""; //
<?php // Initialize variables to null.
$title =""; // Sender Name
$author =''; // Sender's email ID
$date =date('d-m-y'); // Subject of mail
$desc="";//meta description
$keywords="";//meta keywords
$content =""; // Sender's Message
$category="";//chosen category
$pattern1="";//preg_match pattern
$nameError ="";
$contentError ="";
$purposeError ="";
$messageError ="";
$successMessage =""; // On submittingform below function will execute.
$img_dir=$_SERVER["DOCUMENT_ROOT"] . '/practise/grafitti/images/';
$img;
if(isset($_POST['submit'])) { // Checking null values in message.
//check and assign title title
if(empty($_POST["title_post"])){
$nameError = "A title is required";
errors($nameError);
exit();
}
else{
if (preg_match("/^(\w|\s)$/",$_POST['title_post']))
{
$titleError = "Only letters,numbers and white space allowed";
errors($titleError);
}else{
$title=$_POST['title_post'];
}
}
// Checking null values inthe content.
if (empty($_POST["content_post"]))
{
$contentError = "You have not posted any content.<br/> Please do to proceed";
errors($contentError);
exit();
}else {
$content=$_POST["content_post"];
}
//check and assign category
if(!empty($_POST["categories_post"]))
{
$category=$_POST["categories_post"];
}
//Chexk and assign authors name
if (!empty($_POST["author_post"]))
{
$author=$_POST["author_post"];
}
//check and assign value of description
if (!empty($_POST["desc_post"]))
{
$desc=$_POST["desc_post"];
}
//check and assign keywords
if (!empty($_POST["keywords_post"]))
{
$keywords=$_POST["keywords_post"];
}
//process images
if(isset($_FILES["img_post"])){
echo "good to go";
$name=$_FILES["img_post"]["name"];
$tmp_name=$_FILES["img_post"]["tmp_name"];
$type=$_FILES["img_post"]["type"];
$size=$_FILES["img_post"]["size"];
$img_dir;
if(upload($name,$type,$size,$tmp_name,$img_dir)){
if(move_uploaded_file($tmp_name,$img_dir.$name)){
echo "success";
}else{echo php_info;}
$img_upload_Success="File was uploaded successfully";
errors($img_upload_Success);
}else{
$img_upload_Error="File could not be uploaded";
errors($img_upload_Error);
exit();
}
}
echo $title."<br/>";
echo $author."<br/>";
echo $desc."<br/>";
echo $keywords."<br/>";
echo $category."<br/>";
echo $date."<br/>";
}
// Function for filtering input values.function test_input($data)
function errors($err){
echo "<script>
var err='$err'
alert(err)
</script>
";
}
#validate file upload
function upload($fl_name,$fl_type,$fl_size,$fl_tmp_name,$dir){
#check to see if the file is an image or not
if($fl_type!="image/jpeg" && $fl_type!="image/png" && $fl_type!="image/jpg" && $fl_type!="image/gif"){
$typeError="The file type you uploaded is not supported";
errors($fl_type);
exit();
}
#check file size limits
if($fl_size>512000){
$sizeError="Size of the file is too big. Should be at least 500KB";
errors($sizeError);
exit();
}
if(file_exists($dir.$fl_name)){
$existError="Sorry. File already exists";
errors($existError);
exit();
}
}
?>
返回一个假值,但执行了上载()。有人能告诉我如何处理isset问题,以及至少一种显示导致文件无法上载的错误的方法吗使用以下条件:
if($_FILES['img_post']['error'] == 0){
//uplode file
}
这将检查它是空的还是文件已被选中。如果选中,则它将仅上载文件。您可以尝试此操作
if($_FILES['img_post']['error']==0) {
// process
} else {
$error_message = $error_types[$_FILES['img_post']['error']];
// do whatever with the error message
}
有关更多详细信息,请参阅。更改此代码
if(isset($_FILES["img_post"])){
到if(isset($_FILES["img_post"]["tmp_name"])){
使用以下代码:-
if(isset($_FILES["img_post"]["tmp_name"]) && $_FILES["img_post"]["tmp_name"] != ''){
//uplode file
}
或
只需检查
$\u文件[“img\u post”][“tmp\u name”]!=“
与您的isset($\u文件[“img\u post”])
。因此,如果(isset($\u FILES[“img\u post”])和&$\u FILES[“img\u post”][“tmp\u name”!=”){。您好,您可以检查是否为文件组件设置了名称或tmp\u名称。\\u FILES[“img\u post”][“tmp\u name”]或$\u FILES[“img\u post”][“name”]。如果要检查上传成功与否,请检查文件[“img\u post”[“错误”]将返回状态,=0表示成功$\u文件[“img\u post”]
所有文件都已工作,但文件仍无法上载。我认为问题可能是upload()
,因为如果(upload($name,$type,$size,$tmp\u name,$img\u dir))
返回错误结果
if(isset($_FILES["img_post"]["tmp_name"]) && $_FILES["img_post"]["tmp_name"] != ''){
//uplode file
}
if(!empty($_FILES["img_post"]["tmp_name"])){
//uplode file
}