Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: PHP Upload to random dir.

  1. #1
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Upload to random dir.

    I'm making an upload script with PHP
    I want the uploaded files to be uploaded to a random directory.

    I made something like this:
    PHP Code:
    // the important values
    $randit rand(100000999999);
    $dir "imgs/" $randit "/";
    $uploadfile $dir $userfile_name;

    if (
    mkdir("imgs/" $randit) && chmod("imgs/" $randit0777))  // make the dir
    {
    if (
    move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
    {
    // file uploaded URL here

    else 
    {
       echo 
    "Couldn't upload";
    }

    of course this isn't the whole script.. i wrote just the important parts of the script

    after all of this i get "Couldn't upload", the dir was made with 0777 CHMOD but it didn't upload the file
    what is my mistake ?

  2. #2
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    $_FILES['userfile']['tmp_name'] should be $_FILES['uploadedfile']['tmp_name']

  3. #3
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    userfile is the name of the File <input>
    i can't change it.
    anyways, if i try to upload the file without making a random directory it works
    i can't seem why the script doesn't work

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $uploadfile = $dir . $userfile_name;
    should be
    Code:
    $uploadfile = $dir . $_FILES['userfile']['name'];
    No register_globals here, so you're trying to give the file a blank filename, which isn't allowed.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    it's not the whole script...
    i wrote new script but it doesnt work too...
    here it is and this time it's the whole:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <META content="text/html; charset=windows-1255" http-equiv=Content-Type>
    <title>NEOPROJECTS</title>
    </head>
    <body>
    <?php
    include('config.php');
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");

    if (!
    is_writeable($bdir)){
        die (
    "Error: The directory <b>($bdir)</b> is NOT writable");
    }

    $size $canup/1024/1024;
    $rand_dir rand(100000999999);
    $mkdir $bdir."/".$rand_dir."/";
    echo 
    "[<b>".$size." MB</b>]<br>";
    echo 
    "<form enctype=\"multipart/form-data\" method=\"post\" action=\"?action=upload\">
    <input type=\"file\" name=\"userfile\">
    <br>
    <input type=submit name=submit value=Send></form>"
    ;

    if(
    $_GET['action'] == 'upload')
    {

    $file_type $_FILES['userfile']['type'];
    $file_name $_FILES['userfile']['name'];
    $file_name str_replace(" """$file_name);
    $file_name strtolower($file_name);
    $file_size $_FILES['userfile']['size'];
    $file_tmp $_FILES['userfile']['tmp_name'];

    $ext strrchr($file_name,'.');
    $ext str_replace(" """$ext);
    $ext strtolower($ext);

    if(!
    is_uploaded_file($file_tmp))
    {
    echo 
    "Error: Please select a file to upload!";
    exit();
    }

    if ((
    $extlimit == "yes") && (!in_array($ext,$limitedext))) 
    {
    echo 
    "Error: Wrong file extension";
    exit();
    }

    $rand_name rand(1099);

    if(
    $file_size == 0)
    {
    echo 
    "Error: File size is 0";
    exit();
    }
    if(
    $file_size == $canup || $file_size $canup)
    {
    echo 
    "Error: File size is bigger than <b>".$size." MB</b>";
    exit();
    }

    mkdir($mkdir) or die ("Directory could not be created.");
    chmod($mkdir0777) or die ("Directory could not be CHMOD.");

    $mkdir $mkdir.$rand_name.$file_name;

    if (
    move_uploaded_file($file_tmp$mkdir))
        {
    echo 
    "Direct link <input type=text size=45 onFocus=\"this.select()\" value=".$url.$mkdir.">";
    echo 
    "<br>Forums link <input type=text size=45 onFocus=\"this.select()\" value=[IMG]".$url.$mkdir."[/IMG]>";
    } else {
        echo 
    "Error: A problem occurred while trying to upload the file";
        exit();
    }
    }
    ?>
        <br>
        <a href="http://validator.w3.org/check?uri=referer"><img
            src="http://www.w3.org/Icons/valid-html401"
            alt="Valid HTML 4.01 Transitional" height="31" width="88" border="0"></a>
    </body>
    </html>
    this is the config.php :
    PHP Code:
    <? 
    $url
    ='http://eyal.webkit.biz/MsU/';
    $bdir='imgs';
    $canup='5242880';
    $extlimit='yes';
    ?>
    BTW
    The script works good if i give it a specific directory and not a random.

    the idea is to create a random directory and upload the file to it

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Where does that one fail, then?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    mkdir($mkdir) or die ("Directory could not be created."); 
    chmod($mkdir0777) or die ("Directory could not be CHMOD."); 

    $mkdir $mkdir.$rand_name.$file_name

    if (
    move_uploaded_file($file_tmp$mkdir)) 
        { 
    echo 
    "Direct link <input type=text size=45 onFocus=\"this.select()\" value=".$url.$mkdir.">"
    echo 
    "<br>Forums link <input type=text size=45 onFocus=\"this.select()\" value=[IMG]".$url.$mkdir."[/IMG]>"
    } else { 
        echo 
    "Error: A problem occurred while trying to upload the file"
        exit(); 

    this part..
    I get "Error: A problem occurred while trying to upload the file"

  8. #8
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    any help please?

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Add error_reporting(E_ALL); to the top of that code.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ok.. but i get nothing
    maybe there is another way to make a random directory while submitting the form and then to upload the file to this directory?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •