file_exist not working WHEN PLACED inside php function

前端 未结 6 1257
广开言路
广开言路 2021-01-27 15:37

I have searched for almost similar questions but none of those gave me the right answer. I have a fully working file_exist code in if else statement he

相关标签:
6条回答
  • 2021-01-27 16:03

    You can't call the PHP function from HTML/JS. PHP already finished it's work on server side when HTML/JS start working on the client side.

    About file_exists - it doesn't change it's behavior in- or outside of function. So the problem should be in fil path. MAy be it's bacause you have /Project/events/folder-01/event-01.txt in the 1st case and /Alchemy/events/folder-01/event-01.txt in the 2nd? (Alchemy != Project)

    0 讨论(0)
  • 2021-01-27 16:04

    you can't call the PHP function From HTML tag like Javascript Function. you have to change your function to javascript function to invoke it like this

    onclick="readexisting()"
    
    0 讨论(0)
  • 2021-01-27 16:08

    onclick= is used to call javascript function or to run javascript code you can not call a php function using onclick="", but as you are saying you are getting desired output without function is just because when you clicked on image it just render the same page having your first code and when you moved this code to a function, and as functions does not run by its own you need to call them thats why in second case you did not get expected output.

    If you want to call php function using javascript use AJAX

    Calling a PHP function using javascript

    Create a test folder ex. ajax (yourhost/ajax/)

    view.php (yourhost/ajax/view.php)

    <html>
        <head>
            <title>Simple AJAX Example</title>
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        </head>
        <body>
            <div class="Thumb popup_element shadow clearfix" id="u2413"><!-- group -->
              <img class="grpelem" id="u2471" alt="This Week's Events" src="images/blank.gif" onclick="readexisting()"/><!-- state-based BG images -->
            </div>
    
            <script>
                function readexisting() {
                     jQuery.ajax({
                        type: "POST",
                        url: 'controller.php',
                        data: {action: 'readexisting', arguments: 'your data'}, 
                        success:function(data) {
                                             data = data.split("~:~");
                                             alert(data[0]); // message
                                             alert(data[1]); // content
                        }
                    });
                }
            </script>
        </body>
    </html>
    

    controller.php

    <?php
        include_once("model.php");
    
        $obj = new Model();
    
        switch($_POST["action"]){ 
            case 'readexisting': 
                $obj->readexisting();
            break;      
        } 
    ?>
    

    model.php

    <?php
    class Model {
    
       public function readexisting() {
           if (file_exists($_SERVER['DOCUMENT_ROOT']."/Project/events/folder-01/event-01.txt")) {  
                $myFile = ($_SERVER['DOCUMENT_ROOT']."/Project/events/folder-01/event-01.txt");
                $fh = fopen($myFile, 'r');
                $theData = fread($fh, filesize($myFile));
                fclose($fh);
    
                echo "The file exists. ~:~".$theData ;                      
           } else {
                echo "The file $filename does not exist";
           }
       }
    
    }
    ?>
    
    0 讨论(0)
  • 2021-01-27 16:09

    It's not possible to call a PHP-Function with onclick (directly) without reloading/loading the page.

    But you can use <a> tag for alternative solution and call that PHP function.

    0 讨论(0)
  • 2021-01-27 16:14

    You are trying to call a PHP function the same way as a JavaScript function. This just doesn't work.

    onclick="readexisting()"
    

    You can only call JavaScript functions like this.

    0 讨论(0)
  • 2021-01-27 16:15

    With onclick= you assign a JavaScript event handler, not a PHP function.

    I don't know what your code is supposed to do, but perhaps you can execute the function when something is POSTed to your file.

    0 讨论(0)
提交回复
热议问题