views:

44

answers:

2

hi guys,

ive been commissioned to create a website, but one of the requirements is driving me insane.

I have to create (or find) a php based script, that allows a user to login to the domain, upload a html file (+ images or audio files) and also specify the date, at which time the html page will be made accessible? eg. This is to allow the user to upload multiple files before going on a holiday, so that they are automatically released on the correct upload days without user interaction with the domain.

There is a good chance I will have to hit a mysql database at the same time for search indexing with ajax once the page is made available.

I have an idea how to create the php script, 'upload files to tmp dir' but im not sure how to issue a command to move them into the correct directories afterwards based on date/time. please advise.

The server will be either Ubuntu-Server or CentOS based.

+1  A: 

If your server supports cron, you could set up a short bash script to copy a list of files (as arguments) to the directory.

Alternatively, if the site is going to be getting regular hits, it may be simpler to have a PHP function that checks whether any files need copying every time the page is accessed. For speed and simplicity you could have it just check when the last copy was, and if it's more than 24 hours do the more complex stuff like checking the list of files and moving them.

EDIT: I forgot to mention (I'm not sure how much experience you've had, so apologies if this is obvious!), when you upload the files, you'd want to store something in the database like "file, date_to_move, completed", where file is the filename, date_to_move would be the mysql timestamp of the date to move and completed is a boolean so you can cut down you potential query results, i.e:

SELECT * FROM my_table WHERE CURRENT_TIMESTAMP() > date_to_move AND completed = 0

You would then update the table upon the successful move so that any moved files had completed = 1.

ianhales
ive had a chance to mess with cron and it looks like its what I can use, thanks. I may need to revisit the mysql suggestion but I have afew concerns.
JB87
how do I stop visitors viewing say, www.mywebsite.com/tmp (where all the file uploads are held)
JB87
and can I use php and cron to catch or read a future timestamp of the upload file... say by using the touch date function? to represent the upload and move day....
JB87
Hi, apologies I've been away. You can use .htaccess (assuming your host allows) to deny access permissions to folders (the following may be of use - http://www.ducea.com/2006/08/11/apache-tips-tricks-deny-access-to-some-folders/).I don't really understand the second part of your question though, sorry. Could you clarify?
ianhales
thanks for the help, I was asking if there is a way to read the timestamp of a file directly into php. as I can use the linux touch command to change the timestamp of a file (presumably to one that is in the future based on a user form field)
JB87
I've never used it, but the API tells me there's a function called filemtime, which reads the modification date of a file. More details here: http://php.net/manual/en/function.filemtime.php. FYI, there's also a PHP touch function, which seems to do the same as the Linux command but is probably better practice than a system call: http://www.php.net/manual/en/function.touch.php
ianhales
+1  A: 

I will assume that:
1. every user has a specific folder;
2. in a database will be at least a record with user and date/time to be alive;

You can do it like this:

When the user uploads the files you could move it immediatelly to his folder.
If it is the first file submited, create inside the user's folder a file index.php which will check to see if the requested time is grater than the record from the database. If yes, redirect the browser to the html file (or something else).
Make sure that the file uploaded by the user is not named index.php. If yes, rename his file something else and in your generated index make the redirection to the new name.

Parkyprg
thanks his is essentially what im going for, but with cron on the index.php, im still unclear though how to associate a data record with some file, when both the name and date will not be searchable? as the dates are different and the name could change
JB87
In your database you will have something like userID and file. In index.php that you will create you know the userid. Based on that check in the database for the file to redirect. To be sure that noone access any files in that folder before the specified date/time (maybe by typing direct URL) you can also create a .htaccess file in the same time with index.php to forbid any access. When the time comes delete this file from the folder. I hope you understand what I mean and I don't know if this is the best solution but this is how I would do it.
Parkyprg
your answers are very helpful! they are somewhat complex but I am slowing beginning to clearly picture how to make this work. if you could add one or two important lines from your custom index.php file here I think I will have the answer: such as, how it knows what the userID of a new user folder is and the file redirect.
JB87
So, as I said the userID is known. In the index.php you can put like this: $row=mysql_fetch_array(mysql_query('select dateToBeAlive, redirectFilename from <table> where userID=1234/*you will put here the exact id when you are creating the file*/')); if ($row['dateToBeAlive']>=date('Y-m-d')) {if (file_exists($row['.htaccess'])) unlink($row['.htaccess']); header("location:".$row['redirectFilename']);}
Parkyprg
this is an elegant solution. thanks! can you point me in the direction of any good books or articles based on apache that you have found useful. I would like to improve my knowledge of this subject
JB87