• I’ve created a script to add images to categories. Because I like to keep themes modular I built it in a separate file so it can be added to other themes.

    I’ve also built a theme options interface so the user can select whether or not the want the image scroller to appear on the front page, etc. The image scroller pulls from posts by default but the user can also choose to use images from the categories.

    The thing is, I only want to show the option to use category images if the category_images.php file exists. My theme options are built using arrays and if file_exists isn’t compatible with arrays.

    Here’s the (broken) code of what I’m trying to do:

    if (file_exists($categoryImagesFile)) {
    
    array(  "name" => "Use Posts Or Categories",
            "desc" => "Do you want to feature images from featured posts, or use featured category images instead?",
            "id" => $shortname."_slideshow_source",
    	"options" => array('Posts', 'Categories'),
            "std" => "Posts",
            "type" => "select"),
    }

    Any suggestions on a workaround would be greatly appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Does $categoryImagesFile contain the name of the file? If so, you should use that in conjunction with the path to the file.
    http://php.net/manual/en/function.file-exists.php

    any error messages? or is it just ‘not working’?

    http://php.net/manual/en/language.types.array.php

    would you not need an array name?

    also, the komma after the closing bracket of the arry might cause a syntax error.

    what code would you use if you don’t want to check for the existance of the file?

    Thread Starter karmatiger

    (@karmatiger)

    Andrew

    Yes. $categoryImagesFile = $homepath . 'category_images.php';

    alchymyth

    The site crashes when I try to use if file_exists to create an array. in this case the comma after the parenthesis is required as more arrays follow, but in reading up on it the file_exists() function doesn’t seem to like arrays at all.

    The goal here is to see if that file is present and if so create the array, so I’m not sure how I would code that without checking to see if the file exists.

    can you post the full code – not just the section ?

    Thread Starter karmatiger

    (@karmatiger)

    I found one problem – I should be getting the server path not a URL path. I’ve changed:

    $categoryImagesFile = $homepath . 'category_images.php';

    to:

    $basePath = get_bloginfo('template_url');
    $shortPath= parse_url($basePath,PHP_URL_PATH);
    $fullPath = $_SERVER['DOCUMENT_ROOT'] . $shortPath . "category_images.php";

    and the corresponding variable in the if statement:

    if (file_exists($fullPath)) {
    
    array(  "name" => "Use Posts Or Categories",
            "desc" => "Do you want to feature images from featured posts, or use featured category images instead?",
            "id" => $shortName."_slideshow_source",
    		"options" => array('Posts', 'Categories'),
            "std" => "Posts",
            "type" => "select"),
    }

    but still no bun, because file_exists doesn’t play nice with arrays whether the URL is correct or not.

    The full list of arrays is huge and this is the pertinent bit. If I comment out the if and just use the array it works smurfily. I just want to be able to perform the check if the file exists so the panel isn’t offering the user an option that doesn’t exist.

    The full list of arrays is huge

    ok, then please post just a few lines before and after together with the so far posted section;

    I suspect that the error might be a general syntax error and not caused by the conditional statement in particular.

    Thread Starter karmatiger

    (@karmatiger)

    array(  "type" => "preheader",
            "name" => "Featured Content (jQuery slider)"),   
    
    array(  "name" => "Show slider on homepage?",
            "desc" => "Do you want to show the image slider on the homepage?",
            "id" => $shortName."_featured_posts_show",
            "options" => array('Yes', 'No'),
            "std" => "Yes",
            "type" => "select"),
    
    if (file_exists($fullPath)) {
    
    array(  "name" => "Use Posts Or Categories",
            "desc" => "Do you want to feature images from featured posts, or use featured category images instead?",
            "id" => $shortName."_slideshow_source",
    		"options" => array('Posts', 'Categories'),
            "std" => "Posts",
            "type" => "select"),
    } 
    
    array(  "name" => "Autoplay Slideshow?",
            "desc" => "Should the slideshow have enabled auto-rotate?",
            "id" => $shortName."_slideshow_auto",
            "options" => array('Yes', 'No'),
            "std" => "Yes",
            "type" => "select"),

    not optimal, but you could try using a ternary operator:

    ....
            "options" => array('Yes', 'No'),
            "std" => "Yes",
            "type" => "select"),
    
     (file_exists($fullPath)?
    array(  "name" => "Use Posts Or Categories",
            "desc" => "Do you want to feature images from featured posts, or use featured category images instead?",
            "id" => $shortName."_slideshow_source",
    		"options" => array('Posts', 'Categories'),
            "std" => "Posts",
            "type" => "select"):NULL), 
    
    array(  "name" => "Autoplay Slideshow?",
            "desc" => "Should the slideshow have enabled auto-rotate?",
            "id" => $shortName."_slideshow_auto",
    ....

    not ideal, as this would insert NULL into the position, which you need to check to ignore in your ‘foreach’ loop…

    alternative possibly use any of the array function to conditionally add the sub array into the large array; http://www.php.net/manual/en/ref.array.php

    Thread Starter karmatiger

    (@karmatiger)

    still no joy.

    Thanks for trying to help though – I appreciate it.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘create array if file exists?’ is closed to new replies.