File uploading help :(

Discussion in 'PHP' started by rahu_l_, Sep 22, 2011.

  1. #1
    hi friends ..
    i need help :(

    i have a simple script to upload files to server :

    $yourdomain = 'http://domain.com/';
    $uploaddir = 'dl/';
    $filename = $_FILES['file']['name'];
    $filesize = $_FILES['file']['size'];
    $tmpname_file = $_FILES['file']['tmp_name'];
    $date_file = date(imdy);
    if($filesize > '900000000000') {
    echo "Way too big!!";
    } else {
    move_uploaded_file($tmpname_file, "$uploaddir$date_file$filename");
    echo "<br /><br><br>Download Link: <font color=blue><a href=".$yourdomain.$uploaddir.$date_file.$filename.">".$yourdomain.$uploaddir.$date_file.$filename."</font></a>";
    }


    it upload file to "/dl/" and rename the file name to 972839753myfile.zip

    i need to make it domain.com/dl/353465786/myfile.zip

    plz help :(
     
    Solved! View solution.
    rahu_l_, Sep 22, 2011 IP
  2. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Two approaches.

    You can handle this with a rewrite rule. I'm not good enough at writing rewrite rules to help you with this, but something that sees requests to /dl/* and points them to the appropriate file could work.

    Otherwise, http://us.php.net/mkdir
     
    jevchance, Sep 22, 2011 IP
  3. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #3
    $yourdomain = 'domain.com/'; #remove protocol so that we can use it as dir
    $uploaddir = "/$domain/dl/";
    .
    .
    .
    if($filesize > '900000000000') {
        echo "Way too big!!";
    } else {
        move_uploaded_file($tmpname_file, "{$uploaddir}{$date_file$filename}"); 
        #etc etc
    }
    PHP:
    I hope this will work if you have enough permissions to write to said folder.
     
    Vooler, Sep 23, 2011 IP
  4. rahu_l_

    rahu_l_ Well-Known Member

    Messages:
    535
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    175
    #4
    i am super noob in PHP , can you please explain me more clearly :(
     
    rahu_l_, Sep 23, 2011 IP
  5. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #5
    Hi, lt's make it more compact and simple

    
    @mkdir("domain.com/dl/{$date_file}");      #create directory if missing
    $uploaddir = "domain.com/dl/{$date_file}"; # is this the folder you want to write files to ?
    move_uploaded_file($tmpname_file, "{$uploaddir}/{$filename}"); #upload file to desired directory
    
    PHP:
    Is it clear now?
     
    Vooler, Sep 23, 2011 IP
  6. rahu_l_

    rahu_l_ Well-Known Member

    Messages:
    535
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    175
    #6
    Not working :((
     
    rahu_l_, Sep 23, 2011 IP
  7. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #7
    You'll need to post your entire code for us to review. Vooler's example wasn't a complete solution, it was a snippet to get you pointed in the right direction.
     
    jevchance, Sep 23, 2011 IP
  8. rahu_l_

    rahu_l_ Well-Known Member

    Messages:
    535
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    175
    #8
    Check your PM
     
    rahu_l_, Sep 23, 2011 IP
  9. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #9
    Paste the error that you see.

    regards
     
    Vooler, Sep 23, 2011 IP
  10. KsNitro

    KsNitro Greenhorn

    Messages:
    60
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #10
    Be sure your problems with this aren't related to permissions. The code may work, but you need the right file permissions set for it to work correctly. Also, just so you know, file uploads aren't the easiest tasks for beginners. But, it isn't that bad if you spend a little time with it.
     
    KsNitro, Sep 23, 2011 IP
  11. #11
    
        $yourdomain = 'http://www.ougfiles.in/'; 
        $uploaddir = 'dl/'; 
        $filename = $_FILES['file']['name']; 
        $filesize = $_FILES['file']['size']; 
        $tmpname_file = $_FILES['file']['tmp_name']; 
        $date_file = date(imdy); 
        @mkdir("$uploaddir$date_file");
        if($filesize > '900000000000') { 
            echo "Way too big!!"; 
        } else { 
            move_uploaded_file($tmpname_file, "$uploaddir$date_file/$filename"); 
            echo "<br /><br><br>Download Link:  <font color=blue><a href=".$yourdomain.$uploaddir.$date_file.'/'.$filename.">".$yourdomain.$uploaddir.$date_file.$filename."</font></a>"; 
        } 
    
    PHP:
     
    Vooler, Sep 24, 2011 IP
  12. rahu_l_

    rahu_l_ Well-Known Member

    Messages:
    535
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    175
    #12
    Thank You so much dude ....it worked!!!!
    thanks everyone !
     
    rahu_l_, Sep 24, 2011 IP