Jamesonp Posted January 8, 2014 Share Posted January 8, 2014 (edited) 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 January 8, 2014 by Jamesonp Quote Link to comment Share on other sites More sharing options...
GetFreaky Posted January 8, 2014 Share Posted January 8, 2014 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? Quote Link to comment Share on other sites More sharing options...
GetFreaky Posted January 8, 2014 Share Posted January 8, 2014 (edited) // 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 January 8, 2014 by GetFreaky Quote Link to comment Share on other sites More sharing options...
Jamesonp Posted January 8, 2014 Author Share Posted January 8, 2014 Are you looking to find the folder based on a range of dates or 1 specific date? Thanks for the reply. Basically all it needs to do is fill in the current date for the folder. Quote Link to comment Share on other sites More sharing options...
GetFreaky Posted January 8, 2014 Share Posted January 8, 2014 (edited) 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 January 8, 2014 by GetFreaky Quote Link to comment Share on other sites More sharing options...
Jamesonp Posted January 8, 2014 Author Share Posted January 8, 2014 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. Quote Link to comment Share on other sites More sharing options...
GetFreaky Posted January 8, 2014 Share Posted January 8, 2014 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? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 8, 2014 Share Posted January 8, 2014 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"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.