Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:03:12+00:00 2026-05-28T18:03:12+00:00

I’m using PHP and a Cron Job to dynamically update a stylesheet at regular

  • 0

I’m using PHP and a Cron Job to dynamically update a stylesheet at regular intervals (for various reasons, Javascript isn’t an option). This is the script I’m running as a Cron Job:

<?php
$colorstack = json_decode(file_get_contents("colors.txt"));
$color = array_shift($colorstack);
file_put_contents("color.txt",$color);
array_push($colorstack, $color);
$colors = json_encode($colorstack);
file_put_contents("colors.txt", $colors);

$iconstack = json_decode(file_get_contents("icons.txt"));
$icon = array_shift($iconstack);
file_put_contents("icon.txt",$icon);
array_push($iconstack, $icon);
$icons = json_encode($iconstack);
file_put_contents("icons.txt", $icons);

print_r ($colorstack);
print_r ($iconstack);
?>

It returns strings from two text files (a set of hex codes, and a set of image filenames) and puts them into arrays. Then it grabs the first value from each array, writes them to a second set of text files (which are read by css.php), then sticks those values back onto the end of the arrays and writes them back to their text files.

Each time the script is executed, it spits up a new color hex code and image filename for the stylesheet, and sends the previous ones to the back of the loop. I’ve tested it, and it works fine in the browser.

The problem is that the Cron Job won’t execute the script. Instead, I keep getting the following:

Warning: array_shift() expects parameter 1 to be array, null given in /path/to/file.php on line 3

Warning: array_push() expects parameter 1 to be array, null given in /path/to/file.php on line 5

And so on. Obviously the problem is that the Cron Job is not parsing $_____stack = json_decode(file_get_contents("_____.txt")); as an array – and I’m assuming it would do the same for explode(); or similar in place of JSON.

Is there another relatively neat way to get the contents of those text files into arrays that won’t run into the same issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:03:13+00:00Added an answer on May 28, 2026 at 6:03 pm

    This seems to a path issue. The cronjob is run as a system process and is therefore not able to locate the files “colors.txt” and “icons.txt”.

    But when you are executing the script in a browser, it automatically reads the files from the current folder.

    The solution is to provide the full system path for the files in the cronjob. And in general always use full paths when doing any file read-writes in a cron script.

    Here is a sample code:

    $filePath = ''; // set it to full-path of the directory that contains the .txt files and terminate with a "/" (slash)
    ....
    $colorstack = json_decode(file_get_contents($filePath . "colors.txt"));
    ....
    $iconstack = json_decode(file_get_contents($filePath . "icons.txt"));
    

    Hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.