File upload problem

Discussion in 'PHP' started by Dimension, Jan 18, 2008.

  1. #1
    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?
     
    Dimension, Jan 18, 2008 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    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();
     
    HuggyStudios, Jan 18, 2008 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    SmallPotatoes, Jan 18, 2008 IP
  4. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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 :confused:
     
    Dimension, Jan 19, 2008 IP
  5. Denn

    Denn Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    Denn, Jan 19, 2008 IP
  6. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    errr im a total newb at php really. could you post up some code so i know what im doing?
     
    Dimension, Jan 19, 2008 IP
  7. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #7
    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:
     
    HuggyStudios, Jan 20, 2008 IP
  8. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    what is the 'random_number' in this bit..... if($_SESSION['random_number'])

    also, how do i start a session?
     
    Dimension, Jan 21, 2008 IP
  9. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #9
    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?
     
    HuggyStudios, Jan 21, 2008 IP
  10. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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?
     
    Dimension, Jan 24, 2008 IP
  11. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    can anyone help?
     
    Dimension, Jan 27, 2008 IP