My PHP Uploader Questions..

Discussion in 'PHP' started by Jake-Johnson, Apr 10, 2009.

  1. #1
    I've gotten the file to upload, but I want it to be able to randomize the file name instead of the file's actual extension name.

    Like instead of

    /uploads/image.png

    it's

    /uploads/73d92cgj_dwsd.png

    I'm using:

    
    <?php
    $target = "upload/";
    $target = $target . basename( $_FILES['uploaded']['name']) ;
    $ok=1;
    
    //This is our size condition
    if ($uploaded_size > 350000)
    {
    echo "Your file is too large.<br>";
    $ok=0;
    }
    
    //This is our limit file type condition
    if ($uploaded_type =="text/php")
    {
    echo "No PHP files<br>";
    $ok=0;
    }
    
    //Here we check that $ok was not set to 0 by an error
    if ($ok==0)
    {
    Echo "Sorry your file was not uploaded";
    }
    
    //If everything is ok we try to upload it
    else
    {
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
    {
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    }
    else
    {
    echo "Sorry, there was a problem uploading your file.";
    }
    }
    ?>
    
    PHP:
     
    Jake-Johnson, Apr 10, 2009 IP
  2. BlackhatVault

    BlackhatVault Banned

    Messages:
    262
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    BlackhatVault, Apr 10, 2009 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    I usually put a timestamp on the end of my filenames. You could do something like the following;

    
    				// set the target folder for the image
    				$target = $_SERVER['DOCUMENT_ROOT'] . "/files/"; 
    
    				// add a timestamp to the image name
    				$timestamp = date('U');
    				$file_arr = explode(".",$_FILES['file']['name']);
    				$file = $timestamp . "." . $file_arr[1];
    				$target = $target . $file; 
    				if(!(move_uploaded_file($_FILES['attachments']['tmp_name'], $target)))
    
    PHP:
     
    Weirfire, Apr 10, 2009 IP
  4. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    @Weirfire - Yours would work as long as the user didnt have a . in their file name for instance say if they had it like "test.test2.jpg" it would return test2.


    @Jake-Johnson: Here you go buddy... Didn't get to test it but should be good to go .. Leave rep if you use.


    
    <?php
    function createRandomString() 
    {
        $chars = "abcdefghijkmnopqrstuvwxyz023456789";
        srand((double)microtime()*1000000);
        $i = 0;
        $string = '';
        while ($i <= 7) {
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $string = $string . $tmp;
            $i++;
        }
        return $string;
    }
    $newimgname = createRandomString();
    $target = "upload/";
    $ext = strrchr(basename( $_FILES['uploaded']['name']), '.'); 
    $target = $target.$newimgname.$ext;
    $ok=1;
    
    //This is our size condition
    if ($uploaded_size > 350000)
    {
    echo "Your file is too large.<br>";
    $ok=0;
    }
    
    //This is our limit file type condition
    if ($uploaded_type =="text/php")
    {
    echo "No PHP files<br>";
    $ok=0;
    }
    
    //Here we check that $ok was not set to 0 by an error
    if ($ok==0)
    {
    Echo "Sorry your file was not uploaded";
    }
    
    //If everything is ok we try to upload it
    else
    {
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
    {
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    }
    else
    {
    echo "Sorry, there was a problem uploading your file.";
    }
    }
    ?>
    
    PHP:
     
    atlantaazfinest, Apr 10, 2009 IP
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    true - you could change the line

    $file = $timestamp . "." . $file_arr[1];
    PHP:
    to

    $file = $timestamp . "." . $file_arr[count($file_arr)-1];
    PHP:
     
    Weirfire, Apr 10, 2009 IP
  6. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    There you go that would work... hopefully its helps this guy out.
     
    atlantaazfinest, Apr 10, 2009 IP
    Weirfire likes this.
  7. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #7
    Indeed! :)
     
    Weirfire, Apr 10, 2009 IP