php gurus question inside

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chris
    Too lazy to set a custom title
    • May 2003
    • 27880

    #1

    php gurus question inside

    I have an upload script

    Code:
    <?php
    
    // Where the file is going to be placed 
    $target_path = "uploads/";
    
    /* Add the original filename to our target path.  
    Result is "uploads/filename.extension" */
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    $_FILES['uploadedfile']['tmp_name'];  
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    
    ?>

    i want it to remove the spaces in the file name when uploaded

    quick fix??????


    also anytime i upload a file over like 1 meg
    i get There was an error uploading the file, please try again!

    i know what the issue is ... but i increase the size but i think im doing it right


    Code:
    <html>
    <form enctype="multipart/form-data" action="uploader.php" method=hahahahahaha>
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" value="Upload File" />
    </form>
    </html>

    what value would I put for like max 200megs
    [email protected]
  • woj
    <&(©¿©)&>
    • Jul 2002
    • 47882

    #2
    you need to change the setting via php.ini or .htaccess to increase the limit over 1 meg...
    Custom Software Development, email: woj#at#wojfun#.#com to discuss details or skype: wojl2000 or gchat: wojfun or telegram: wojl2000
    Affiliate program tools: Hosted Galleries Manager Banner Manager Video Manager
    Wordpress Affiliate Plugin Pic/Movie of the Day Fansign Generator Zip Manager

    Comment

    • Chris
      Too lazy to set a custom title
      • May 2003
      • 27880

      #3
      Originally posted by woj
      you need to change the setting via php.ini or .htaccess to increase the limit over 1 meg...
      seriously? lol
      default server is liek 1 meg or something?
      [email protected]

      Comment

      • woj
        <&(©¿©)&>
        • Jul 2002
        • 47882

        #4
        create a phpinfo.php file and put <?php phpinfo(); ?> in it, it will tell you bunch of stuff about php, including your current upload file size limit...
        Custom Software Development, email: woj#at#wojfun#.#com to discuss details or skype: wojl2000 or gchat: wojfun or telegram: wojl2000
        Affiliate program tools: Hosted Galleries Manager Banner Manager Video Manager
        Wordpress Affiliate Plugin Pic/Movie of the Day Fansign Generator Zip Manager

        Comment

        • Chris
          Too lazy to set a custom title
          • May 2003
          • 27880

          #5
          damn woj you a gangsta

          sho nuff max upload 2 megs

          now time to break my server and edit that lol
          [email protected]

          Comment

          • woj
            <&(©¿©)&>
            • Jul 2002
            • 47882

            #6
            and on a sidenote, be careful with a script like that, what you have there is not very secure, some loser can upload some script instead of a pic, letting him do whatever he wants on your server
            Custom Software Development, email: woj#at#wojfun#.#com to discuss details or skype: wojl2000 or gchat: wojfun or telegram: wojl2000
            Affiliate program tools: Hosted Galleries Manager Banner Manager Video Manager
            Wordpress Affiliate Plugin Pic/Movie of the Day Fansign Generator Zip Manager

            Comment

            • Tempest
              Too lazy to set a custom title
              • May 2004
              • 10217

              #7
              Originally posted by Chris
              i want it to remove the spaces in the file name when uploaded
              Code:
              $filename=preg_replace("/\s+/g","",$filename);

              Comment

              • Chris
                Too lazy to set a custom title
                • May 2003
                • 27880

                #8
                Originally posted by woj
                and on a sidenote, be careful with a script like that, what you have there is not very secure, some loser can upload some script instead of a pic, letting him do whatever he wants on your server
                yeah gonna throw it behind some htaccess
                [email protected]

                Comment

                • Chris
                  Too lazy to set a custom title
                  • May 2003
                  • 27880

                  #9
                  Originally posted by Tempest
                  Code:
                  $filename=preg_replace("/\s+/g","",$filename);
                  hmm
                  i get an error
                  anywhere in partical i'd put that?
                  [email protected]

                  Comment

                  • micker
                    Confirmed User
                    • Nov 2005
                    • 748

                    #10
                    as for removing spaces... you could always do urlencode($filename)

                    Comment

                    • Chris
                      Too lazy to set a custom title
                      • May 2003
                      • 27880

                      #11
                      hmm

                      i changed it in php.ini but still says 2M in the phpinfo.php file


                      do i gotta restart server
                      [email protected]

                      Comment

                      • Chris
                        Too lazy to set a custom title
                        • May 2003
                        • 27880

                        #12
                        upload_max_filesize 100M 100M

                        :-D fixed

                        restarted apache instead of whole server
                        woo
                        [email protected]

                        Comment

                        • Chris
                          Too lazy to set a custom title
                          • May 2003
                          • 27880

                          #13
                          hrmm it didnt seem to fix it woj
                          [email protected]

                          Comment

                          • heywood
                            So Fucking Banned
                            • Jun 2003
                            • 468

                            #14
                            you could also do a string replace, I like them much better

                            $value = str_replace(" ", "", $value);

                            Pretty simple stuff.

                            As far as the php.ini changes, you do have to restart apache. There is also a browser limitation that you'll run into, as well as timeout issues, with files that large.

                            Comment

                            • Chris
                              Too lazy to set a custom title
                              • May 2003
                              • 27880

                              #15
                              Originally posted by heywood
                              you could also do a string replace, I like them much better

                              $value = str_replace(" ", "", $value);

                              Pretty simple stuff.

                              As far as the php.ini changes, you do have to restart apache. There is also a browser limitation that you'll run into, as well as timeout issues, with files that large.
                              i am smashing my head against the wall with this one
                              [email protected]

                              Comment

                              • heywood
                                So Fucking Banned
                                • Jun 2003
                                • 468

                                #16
                                Also, for your target path, make sure its a full path. Will make life easier.

                                Ex. /home/yoursite/public_html/uploads

                                Comment

                                • heywood
                                  So Fucking Banned
                                  • Jun 2003
                                  • 468

                                  #17
                                  Originally posted by Chris
                                  i am smashing my head against the wall with this one
                                  This should simplify it a bit.

                                  Now I'm not sure, but removing the spaces might actually cause it to fail. It may need to retain those spaces to know where the file is.

                                  Code:
                                  <?php
                                  
                                  // Where the file is going to be placed 
                                  $target_path = "uploads/";
                                  
                                  /* Add the original filename to our target path.  
                                  Result is "uploads/filename.extension" */
                                  
                                  $filename = $_FILES['uploadedfile']['name'];
                                  
                                  $tempfilename = $_FILES['uploadedfile']['tmp_name'];
                                  
                                  $tempfilename = str_replace(" ","", $_FILES['uploadedfile']['tmp_name']);
                                  
                                  $target_path = "uploads/";
                                  
                                  $target_path = $target_path . basename($filename); 
                                  
                                  
                                  
                                  $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
                                  
                                  if(move_uploaded_file($tempfilename, $target_path)) {
                                      echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
                                  } else{
                                      echo "There was an error uploading the file, please try again!";
                                  }
                                  
                                  ?>
                                  One thing you could do is, put a file renaming function in there.
                                  Last edited by heywood; 08-10-2006, 07:55 PM.

                                  Comment

                                  • Chris
                                    Too lazy to set a custom title
                                    • May 2003
                                    • 27880

                                    #18
                                    Originally posted by heywood
                                    This should simplify it a bit.

                                    Now I'm not sure, but removing the spaces might actually cause it to fail. It may need to retain those spaces to know where the file is.

                                    Code:
                                    <?php
                                    
                                    // Where the file is going to be placed 
                                    $target_path = "uploads/";
                                    
                                    /* Add the original filename to our target path.  
                                    Result is "uploads/filename.extension" */
                                    
                                    $filename = $_FILES['uploadedfile']['name'];
                                    
                                    $tempfilename = $_FILES['uploadedfile']['tmp_name'];
                                    
                                    $tempfilename = str_replace(" ","", $_FILES['uploadedfile']['tmp_name']);
                                    
                                    $target_path = "uploads/";
                                    
                                    $target_path = $target_path . basename($filename); 
                                    
                                    
                                    
                                    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
                                    
                                    if(move_uploaded_file($tempfilename, $target_path)) {
                                        echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
                                    } else{
                                        echo "There was an error uploading the file, please try again!";
                                    }
                                    
                                    ?>
                                    One thing you could do is, put a file renaming function in there.


                                    hmm
                                    did that and it renamed the file to

                                    s3v%20c.jpgs3v%20c.jpg

                                    lol

                                    ah well ill look at this with a fresh head tommrow
                                    [email protected]

                                    Comment

                                    • Brujah
                                      Beer Money Baron
                                      • Jan 2001
                                      • 22157

                                      #19
                                      preg_replace is a good option but you can consider renaming every file to a unique string too.

                                      This line:
                                      Code:
                                      $tempfilename = str_replace(" ","", $_FILES['uploadedfile']['tmp_name']);
                                      Would probably be better as something like this:
                                      Code:
                                      $tempfilename = preg_replace('/[^a-z0-9\ ]+/i','',$_FILES['uploadedfile']['tmp_name']);
                                      or this:
                                      Code:
                                      $tempfilename = uniqid('upload-');
                                      http://us2.php.net/uniqid

                                      Looks like you're using move_uploaded_file incorrectly. You want the first value to be the actual file that was uploaded, and the second value to be the new destination and name for it.

                                      So, should be:
                                      Code:
                                      $target_path = $target_path . $tempfilename; 
                                      
                                      if( move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
                                      Depending on your use for this, you might also want to add a test in there to see if the file exists already first before overwriting it.

                                      Comment

                                      • Brujah
                                        Beer Money Baron
                                        • Jan 2001
                                        • 22157

                                        #20
                                        Full code would then look something closer to this (Untested):
                                        Code:
                                        <?php
                                        
                                        # Full path to uploads, end with forward slash
                                        $target_path = "/path/to/uploads/";
                                        
                                        if( !is_uploaded_file($_FILES['uploadedfile']['tmp_name']) ) {
                                        	die("Invalid upload.");
                                        }
                                        
                                        $target_path = $target_path .$_FILES['uploadedfile']['tmp_name']; 
                                        
                                        # Valid characters in filename are a-z, 0-9, or a dot
                                        $tempfilename = preg_replace('/[^a-z0-9\.]+/i','',$_FILES['uploadedfile']['tmp_name']);
                                        # Uncomment below if desiring a unique string filename instead
                                        # $tempfilename = uniqid('upload-');
                                        
                                        $target_path = $target_path . $tempfilename; 
                                        
                                        if( move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path) ) {
                                            echo "The file ".$_FILES['uploadedfile']['tmp_name']." has been uploaded to: ".basename($target_path);
                                        } else {
                                            echo "There was an error uploading the file, please try again!";
                                        }
                                        
                                        ?>

                                        Comment

                                        Working...