Php内容插入页面不起作用

时间:2016-02-26 04:45:00

标签: 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 =""; // 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();
      }
  }
?>

问题是,当我想用​​文件上传验证时。如果我没有上传任何内容,代码仍假定我的 $ _ FILES ['img_post'] isset,因此它运行满足该条件的代码。 此外,如果我设法设置 $ _ FILE 变量,它仍然无法上传。它像

if(upload($name,$type,$size,$tmp_name,$img_dir))

返回false值,但执行了 upload()。有人请告诉我如何处理isset问题,至少有一种方法可以显示导致文件无法上传的错误< / p>

4 个答案:

答案 0 :(得分:0)

使用以下条件:

message: "Shared class "basePath" has no method handling GET /containerABC/files/folder1/myfolderAbc=myfolderName"
name: "Error"
stack: "Error: Shared class "basePath" has no method handling GET /containerABC/files/folder1/myfolderAbc=myfolderName↵    at restRemoteMethodNotFound (d:\NodeJSProjects\abc\node_modules\loopback\node_modules\strong-remoting\lib\rest-adapter.js:322:17)↵    at Layer.handle [as handle_request] (d:\NodeJSProjects\abc\node_modules\loopback\node_modules\express\lib\router\layer.js:95:5)↵    at trim_prefix (d:\NodeJSProjects\abc\node_modules\loopback\node_modules\express\lib\router\index.js:312:13)↵    at d:\NodeJSProjects\abc\node_modules\loopback\node_modules\express\lib\router\index.js:280:7↵    at Function.process_params (d:\NodeJSProjects\abc\node_modules\loopback\node_modules\express\lib\router\index.js:330:12)↵    at next (d:\NodeJSProjects\abodeadmin\node_modules\loopback\node_modules\express\lib\router\index.js:271:10)↵    at Function.handle (d:\NodeJSProjects\abc\node_modules\loopback\node_modules\express\lib\router\index.js:176:3)↵    at router (d:\NodeJSProjects\abc\node_modules\loopback\node_modules\express\lib\router\index.js:46:12)↵    at Layer.handle [as handle_request] (d:\NodeJSProjects\abc\node_modules\loopback\node_modules\express\lib\router\layer.js:95:5)↵    at trim_prefix (d:\NodeJSProjects\abc\node_modules\loopback\node_modules\express\lib\router\index.js:312:13)"
status: 404
statusCode: 404

这将检查它是否为空或文件被选中。如果选中,则只会上传文件。

答案 1 :(得分:0)

你可以试试这个。

json is {
  "error" : {
      "message" : "Unsupported source URL: <UIImage: 0x140053200> size {2448, 3264} orientation 3 scale 1.000000"
  }
}

有关详细信息,请参阅this

答案 2 :(得分:0)

更改此代码

if(isset($_FILES["img_post"])){


if(isset($_FILES["img_post"]["tmp_name"])){

答案 3 :(得分:0)

使用以下代码: -

if(!empty($_FILES["img_post"]["tmp_name"])){
   //uplode file
}

OR

train = [('being at a sports game', '1'), ('selecting group members for a group project', '2'), ('interacting with overly dressed people', '1'), ('partnering up with a stranger for a class', '3'), ('having your presentation criticized by an audience member', '4'), ('being in a situation you cannot control', '3')]