-1

I'm trying to build a simple web application with two parts.

Part A: User gets a link. On that link, there is just one button. When he clicks that button, three things should happen:

1) He downloads a file from a server. 2) Run a php script that will save the time of the download to a database (for that user). 3) Redirect to another page -> Part B

Part B: On the second page, there are two buttons. Browse button to select file from the disk. Upload button to upload the file to the server. There are again three things to this part.

1) File is uploaded and saved on the server. 2) The time of the upload is inserted into database (for that user). 3) If the user tries to open the link (from part A), he is automatically redirected to part B. So there is no way to get to the part A download the file again.

Extra part: 1) I would like to have a timer that measures time that passed since the file was downloaded. This timer will be displayed on the page B, updates every second. 2) If possible, this timer will disable upload option (let's say after 120 minutes). 2) Should I be concerned about security (https)?

I am beginner in PHP. From what I read I would have to combine both JS and PHP, to make this work. Is that correct? How would you build this app?

EDIT - SOLUTION:

I have the solution. Wanted to post it here, but it is way too long. In case you are interested, you can PM me and I will send it to you.

Jan Horčička
  • 671
  • 1
  • 11
  • 26

1 Answers1

0

You can feed user the file setting headers and content in the response, and do database related stuff too, so your link can direct to download.php instead to file download. This way you have more controll over it and It will make it work even if user has javascript disabled.

See this thread PHP xlsx header

If the file user downloads is large then you could think about XSendFile, so you won't need to read file by PHP and then write its content in response with headers. With xsendfile you would send a header and file would be send to a user in response internally by http server so php doesn't need to read the file and then print it into response.

For the timer, you would use javascript, so you're right about that you need php and js. You can also display upload expire date, and the countdown would be nice addition. If you expire upload after x seconds I would consider some additional time for file upload time and page load time, or use $_SERVER['REQUEST_TIME'].

REQUEST_TIME is:

The timestamp of the start of the request. Available since PHP 5.1.0.

So you can calculate time differences to check if upload time is past expire time.

Mr_KoKa
  • 598
  • 4
  • 13
  • Thank you very much for your answer. I tried the code in the link you posted and it downloads and everything, but it doesn't redirect to the new page. (Part A, point 3). Also, how would you solve redirecting every other time, when user open the link (part B, point 3)? I have to admit, I didn't really understand your paragraph "If the file user downloads.......and then print it into response". What did you want to tell there? – Jan Horčička Sep 10 '17 at 11:17
  • Yes, I ate some words :) I was telling about large files, if file is large then you can use xsendfile to pass file trough http server instead of reading it by php and then printing the contents after headers. It seem to be inposible to download a file and redirect in one response, but check this thread https://stackoverflow.com/questions/822707/php-generate-file-for-download-then-redirect To redirect user to other page without downloading a file you would need somehow mark user when one is downloading a file, maybe use cookie or session. – Mr_KoKa Sep 10 '17 at 15:06
  • So I solved by using JS to make an Ajax request. :) I will post solution here when everyting is done. I have a last question: Let's say I have a page, e.g. www.domain.com/index.html I want people to be able to display that page with parameter "user". Eg. www.domain.com/index.html&user=12345 or www.domain.com/index.html&user=34567 and I want them to display that page (still the same, because only one www.domain.com/index.html exists). Then I want to be able to work with the user parameter with JS or PHP. Can this be done? Can it be done on HTML page? Or does it have to .php page? – Jan Horčička Sep 11 '17 at 21:01
  • Or maybe better question: I have an user id (eg 12345). He goes to index.html with this ID. How do I pass this ID among pages. index.html -> download.html -> upload.html ? I want to transfer the information (user number) from one page to another. How can I do that? – Jan Horčička Sep 11 '17 at 21:08
  • You can try to use url rewriting so index.html would be rewritten to index.php without user see that and use php. You could use html and a cookie, or php and session which is essentially a cookie that you show to php and php will session variable array. Make sure that cookie, either session cookie or any other cookie will have correct expiration time so it won't expire before upload (I guess this is what it is about, recognizing user that downloaded file and uploaded it back again). You could also embed user id in the file user is downloading and then uploading back. (first param is ? not &). – Mr_KoKa Sep 11 '17 at 22:03
  • Can you tell me how to solve this issue? I have a form, where user select a file from the disk and then clicks on the submit button to upload the file. I want to save the file on server (using POST i guess). But I also need user's ID to save it with proper name. User's ID is in URL. But it is a html page. Therefore I can get the ID only by Javascript. And then I do not know how to combine the JS with the PHP form. And I need the form to select the file and upload it. Any idea? – Jan Horčička Sep 24 '17 at 17:23
  • You can set hidden field with id using javascript , make some validation that if there is no id the form wont be sent and check if id is set serverside. – Mr_KoKa Sep 25 '17 at 01:50
  • Do you know how to download a file from a folder when I do not know what the file name is? I know just the name of the folder and that there will be exactly one file, that I want to download. – Jan Horčička Oct 16 '17 at 21:08
  • You mean user enters http://example.com/1b6f78e3/ url and it downloads file from it? If so I would think about to change it to something like http://example.com/file/1b6f78e3/ and then use rewrite so it would redirect the request to php script that would provide proper file to download. It might look like `RewriteRule ^file/(\d+)/?$ /script.php?fileid=$1 [L]`. It is apache example, nginx has its own thing for rewrite urls. – Mr_KoKa Oct 17 '17 at 18:31
  • Nono, this doesn't have to do anything with the user. Just plain and simple index.html, where I have button. When I click on the button, I want PHP or JS or HTML to download a file. The problem is, I don't know what is the name of the file (because somebody else uploaded it on the server). Do you know how to make dynamic? Solution I came up with: They upload it to a directory. So the PHP or JS script will just look into that directory and download the only file that is there. Do you know, how to do it? – Jan Horčička Oct 17 '17 at 19:56
  • If you stick to index.html then you cannot use php there, you would need to make ajax call using js to a php script that would return the file name, or just use index.php and do that there. – Mr_KoKa Oct 18 '17 at 18:33