1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

file_exists problem that defies logic

Discussion in 'PHP' started by sarahk, Mar 14, 2012.

  1. #1
    I've got a site which calls "elements" using some pretty straightforward code
    foreach($paths->viewPaths as $path) {            
       if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
                    $elementFileName = $path . 'elements' . DS . $name . $this->ext;
                     return $this->_render($elementFileName, array_merge($this->viewVars, $params), false);
                }
       else debug("No file at: ".$path . 'elements' . DS . $name . $this->ext);
      }
      return "(Error rendering Element: {$name})";
    Code (markup):
    All the viewpaths does is allow me to have different themes in specific circumstances but it's the file_exists that is doing all the work. All my elements have ALWAYS worked and I've been using this particular file for quite some time.

    If I run a straight opendir/readdir script copied from php.net the file is found and a file_exist returns true for the files listed

    when I echo out
    $path . 'elements' . DS . $name . $this->ext
    Code (markup):
    and then add that to my test script the file is found.

    Any ideas why this script is falling over?

    edit: have renamed the element and it's back up and running with no code changes - however I've done this before and therefore can't rely on this "fix"
     
    Last edited: Mar 14, 2012
    sarahk, Mar 14, 2012 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    You have a debug function there, which I'm assuming you use to log errors to a file...can you copy and paste the relating lines (No file at:...) in this specific scenario?

    I could be wrong but the DS constant is the directory separator, which I know could do some funny things as it varies from OS.

    edit: Is this from Cake?

    The following is from there:

    /**
     * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
     *
     * This realizes the concept of Elements, (or "partial layouts")
     * and the $params array is used to send data to be used in the
     * Element.
     *
     * @link
     * @param string $name Name of template file in the/app/views/elements/ folder
     * @param array $params Array of data to be made available to the for rendered view (i.e. the Element)
     * @return string Rendered output
     */
        function renderElement($name, $params = array(), $loadHelpers = false) {
    
            if (isset($params['plugin'])) {
                $this->plugin = $params['plugin'];
                $this->pluginPath = 'plugins' . DS . $this->plugin . DS;
                $this->pluginPaths = array(
                    VIEWS . $this->pluginPath,
                    APP . $this->pluginPath . 'views' . DS,
                );
            }
    
            $paths = Configure::getInstance();
            $viewPaths = am($this->pluginPaths, $paths->viewPaths);
    
            $file = null;
            foreach ($viewPaths as $path) {
                if (file_exists($path . $this->themeElement . $name . $this->ext)) {
                    $file = $path . $this->themeElement . $name . $this->ext;
                    break;
                } elseif (file_exists($path . $this->themeElement . $name . '.thtml')) {
                    $file = $path . $this->themeElement . $name . '.thtml';
                    break;
                } elseif (file_exists($path . 'elements' . DS . $name . $this->ext)) {
                    $file = $path . 'elements' . DS . $name . $this->ext;
                    break;
                } elseif (file_exists($path . 'elements' . DS . $name . '.thtml')) {
                    $file = $path . 'elements' . DS . $name . '.thtml';
                    break;
                }
            }
    
            if (is_null($file)) {
                $file = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'elements' . DS . $name. '.ctp');
            }
    
            if ($file) {
                $params = array_merge_recursive($params, $this->loaded);
                return $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers);
            }
    
            if (!is_null($this->pluginPath)) {
                $file = APP . $this->pluginPath . $this->themeElement . $name . $this->ext;
            } else {
                $file = VIEWS . $this->themeElement . $name . $this->ext;
            }
    
            if (Configure::read() > 0) {
                return "Not Found: " . $file;
            }
        }
    
    PHP:
    Which may be of use, as the code seems similar.
     
    Last edited: Mar 18, 2012
    danx10, Mar 18, 2012 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #3
    Yes, it's cake. The version I'm using is a bit different from yours but even when I go through nightly downloads there were some significant changes to view.php

    Standard output I get is


    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/views/elements/qevent.thtml
    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/views/errors/elements/qevent.thtml
    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/themes/www/elements/qevent.thtml
    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/themes/www/errors/elements/qevent.thtml
    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/themes/elements/qevent.thtml
    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/themes/errors/elements/qevent.thtml
    [COLOR=#000000][FONT=Verdana]orem ipsum[/FONT][/COLOR]
    
    [COLOR=#000000][FONT=Verdana](Error rendering Element: qevent)[/FONT][/COLOR]
    
    about to debug
    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/views/elements/editimageframe.thtml
    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/views/errors/elements/editimageframe.thtml
    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/themes/www/elements/editimageframe.thtml
    No file at: /var/www/vhosts/mysite.org/httpdocs/mysite/app/themes/www/errors/elements/editimageframe.thtml
    found: /var/www/vhosts/mysite.org/httpdocs/mysite/app/themes/elements/editimageframe.thtml
    Code (markup):
    I call the element 4 times and only 1 time fails. I'm going to go through the component that generates the data incase something is breaking in there and fouling the system.
     
    sarahk, Mar 18, 2012 IP