Jump to content

Including date in file path


Jamesonp

Recommended Posts

Hey all,

 

Thanks for this great forum.  I've learned a lot.

 

What I'm trying to do is create a php script that will select a folder based on the date.  For example, this is the original code:

$file = '/var/www/html/public_html/svsshc/'.$_REQUEST['id'].'.png';

the folder the .png files will be in, for example, is:

/var/www/html/public_html/svsshc/2014-01-07

I tried doing this but it did not work properly:

$file = '/var/www/html/public_html/svsshc/'$_date'/'.$_REQUEST['id'].'.png';

I apparently don't have a very good grasp on how the php date function works.  Any suggestions on what to do?

 

Thank you!

Edited by Jamesonp
Link to comment
Share on other sites

Hey all,

 

Thanks for this great forum.  I've learned a lot.

 

What I'm trying to do is create a php script that will select a folder based on the date.  For example, this is the original code:

$file = '/var/www/html/public_html/svsshc/'.$_REQUEST['id'].'.png';

the folder the .png files will be in, for example, is:

/var/www/html/public_html/svsshc/2014-01-07

I tried doing this but it did not work properly:

$file = '/var/www/html/public_html/svsshc/'$_date'/'.$_REQUEST['id'].'.png';

I apparently don't have a very good grasp on how the php date function works.  Any suggestions on what to do?

 

Thank you!

 

 

Are you looking to find the folder based on a range of dates or 1 specific date? 

Link to comment
Share on other sites

// url variables
$date   = '2014-01-07'; // you need to specify the date, and make sure its legal characters
$filename   = $_REQUEST['id']; // the filename (not sure why your specifying the extension in your code

// construct url
$folder = realpath("/var/www/public_html/$data/"); // use dirname if your using clean url
$file   = $folder . $filename;

// check if folder exists

if(is_dir($folder) && is_readable($folder))
 {

    if(file_exists($file))
    {
    
      // do code here.

    }

 }

You need to check if the folder exists and is readable, then the rest is up to you.

Edited by GetFreaky
Link to comment
Share on other sites

Thanks for the reply.  Basically all it needs to do is fill in the current date for the folder.

 

What you are trying to accomplish with the way you have implemented two variables into the url won't work, because one or the other may not exist, you need to test certain conditions before stamping a direct/file/ as existent, and readable. Firstly you need to check if the directory exists, then check if its readable, followed by checking if the file exists. If all those conditional statements prove to be true, then you can construct your url as such 

$file = '/var/www/html/public_html/svsshc/'$_date'/'.$_REQUEST['id'].'.png';

Also where is your $_date variable coming from? Is it from a database,user input, external data, local...etc

Edited by GetFreaky
Link to comment
Share on other sites

No, I was just trying to have php generate the current date in the format of yyyy-mm-dd.  I have a bash script that creates the folder on a daily basis and syncs the necessary files there using lftp.

 

I misunderstood, thats why I was wondering why your code looks a little funny to me, but that makes sense what you are trying to do.

 

When you say

 

 

What I'm trying to do is create a php script that will select a folder based on the dat

 

I thought you ment these folders already exist.

 

You need to use the mkdir function to create the folder, now I'm confused as to what the png is for, is this a file that already exists based on a ID, or is this a new png picture you are generating with php?

Link to comment
Share on other sites

i recommend that you echo your $file variable so that you can see what it actually contains (the $_date variable isn't being replaced by its value.)

 

you would need to concatenate the variable, the same as the $_REQUEST variable -

$file = '/var/www/html/public_html/svsshc/'.$_date.'/'.$_REQUEST['id'].'.png';

or more simply, since it tends to reduce syntax problems, just use an over-all double-quoted string - 

 $file = "/var/www/html/public_html/svsshc/$_date/{$_REQUEST['id']}.png";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.