2

I need to develop a little PHP script that I can run from a cron job which in pseudo code does the following:

//THIS IS PSEUDO CODE
If(file exists with name 'day.jpg')
    rename it to 'fixtures.jpg'
else
    copy 'master.jpg' to 'fixtures.jpg'

Where day.jpg should be the current day of the month.

I started to replace the pseudo code with the stuff I'm pretty sure how to do:

<?php

    if(FILE EXISTS WITH NAME DAY.JPG) {
        rename ("DAY.JPG", "fixtures.jpg");
    } else {
        copy ("master.jpg", "fixtures.jpg");
    }

?>

Clearly there are still a few things missing. Like I need to get the filename with the current day of the month and I need to check if the file exists or not.

I guess I need to do something like this $filename='date('j');'.jpg to get the filename, but it isn't really working so I kinda need a bit help there. Also I don't really know how to check if a file exists or not?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Henry Aspden
  • 1,863
  • 3
  • 23
  • 45

2 Answers2

1
$path = __DIR__; // define path here
$fileName = sprintf("%s%d.jpg", $path, date("j"));
$fixtures = $path . DIRECTORY_SEPARATOR . "fixtures.jpg";
$master   = $path . DIRECTORY_SEPARATOR . "master.jpg";

file_exists($fileName) ? rename($fileName, $fixtures) : copy($master, $fixtures);

Basicly you need script like above but you need to work on your path. Your code above had syntax problem.

Robert
  • 19,800
  • 5
  • 55
  • 85
-1

You have a basic syntax problem, it should be:

$filename = date('j') . '.jpg';

You don't put function calls inside quotes, you need quotes around the literal string '.jpg', and you need to use . to concatenate them.

I recommend you read the chapter on Strings in a PHP tutorial.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks Barmar thats a big help... quick one tho, when do I use == vs just =? – Henry Aspden Sep 08 '16 at 22:22
  • 1
    @HenryAspden If you needed that, you **really** need to read a PHP tutorial from beginning to front, not just the strings chapter. – Barmar Sep 08 '16 at 22:27
  • I've posted an update to my code above, will somebody please take a quick look at it now. Any links to explain where and why my syntax etc is wrong would be really helpful :) As you can see I'm new to PHP. Don't really know where or how to start teaching myself the language other than project by project – Henry Aspden Sep 08 '16 at 22:27
  • You shouldn't put the variable assignment inside `if()`. If you want to know if the file exists, use `if(file_exists($filename))` – Barmar Sep 08 '16 at 22:28
  • @Barmar... PHP tutorial you say? – Henry Aspden Sep 08 '16 at 22:28
  • 1
    Yes, you clearly need to learn how to program in PHP, you don't know some of the simplest concepts. – Barmar Sep 08 '16 at 22:29
  • I appreciate it must be frustating to read my novice codes haha... I am trying to learn hence why I didn't just code dump and expect the answer... this time – Henry Aspden Sep 08 '16 at 22:30
  • 1
    You can't learn to program by trial and error like this, you need to get a good textbook or tutorial. Don't ask me for a recommendation, I don't know. – Barmar Sep 08 '16 at 22:33
  • @Barmar thanks anyway buddy... might look at some apps I can do on my phone and see if there are any good'uns... What do you think to UPDATE 2 if you don't mind me bombarding you? – Henry Aspden Sep 08 '16 at 22:35
  • works like a dream guys... time to start at the basics after this one – Henry Aspden Sep 08 '16 at 22:43