I am trying to make an upload application in php. Its pretty much there however I have hit a problem. When the file is uploaded, it puts in in uploads/[date]/[time] so that each upload has a unique folder down to the second. The problem I have is that when the page is refreshed, it makes another folder because of the date function changing the seconds and the web server could potentially be filled up. Can anyone tell me a way around this?
Change the timestamp part to a unique random id like, $id = rand(1000,100000); Then you can check if a folder already is there, like, if(file_exists("$date/$random_number")) { echo "Already here!"; }else{ // add it } PHP: Thats if it is a folder you are adding, otherwise use if_file();
Every hundred runs you could have it go through and look for empty folders, and delete them. Or store the name of the folder in a session variable so it will get re-used. Or invent a random name for the folder BEFORE drawing the upload form, and provide it in a hidden field. Once the file is uploaded, check that the folder name is secure and then create it.
It has worked but everytime I refresh the page, it is just creating another folder with a new random number. I know this is because the form posts back to the same page but I don't seem to be able to find a way around it
Set a SESSION variable when it's uploaded the first time. Then you check when you upload if th SESSION variable is set, and if it is, do you stuff you want in the same folder.
Something like this, so what we do is store the folder number if it is created into a session, then when they post the add we can check if this data is being submitted again. Also you must place session_start() right at the top of your script before any html is outputted. if($_POST['submit']) { // they pressed submit if($_SESSION['random_number']) { $_SESSION['random_number']=$random_number; if(file_exists($date/$random_number)) { // check if already there echo "Already been made stop refreshing the page!"; } }else{ // do your adding folders etc here $_SESSION['random_number']=$random_number; } } PHP:
what is the 'random_number' in this bit..... if($_SESSION['random_number']) also, how do i start a session?
As I said, You need to place session_start() at the top of the script before any html is output. I was showing you how to go about doing this, I am not going to do it for you. Use google and find out about making random numbers in PHP. Otherwise what are you going to learn?
ok i've given it a go but not getting too far. i have my session_start(); at the very top of the document and then this is what i have so far... if ($_POST["Submit"]) { //setup session as random number $_SESSION['random_number'] = rand(1000,100000); //set the session random number to random_number var $random_number=$_SESSION['random_number']; //think i have got completely lost here if($random_number) { if (!file_exists("upload/" . date("m-d-y"))) { $newdatedir = mkdir("upload/" . date("m-d-y"), 0777); } if(file_exists("upload/" . date("m-d-y") . "/" . $random_number)) { echo "Already been made stop refreshing the page!"; } } else { $newtimedir = mkdir("upload/" . date("m-d-y") . "/" . $random_number, 0777); } } PHP: could anyone tell me where im going wrong?