Web Analytics Made Easy -
StatCounter Numbers appearing on the wrong side of filename - CodingForum

Announcement

Collapse
No announcement yet.

Numbers appearing on the wrong side of filename

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Numbers appearing on the wrong side of filename

    I'm using this code:

    PHP Code:
       $file12 $_FILES['site_url']['name']; // Get the name of the file (including file extension).
       
    $file12 preg_replace('/\s/''_'$file12);
       
    $filename $file12.$rand;
       
    $ext substr($filenamestrpos($filename,$rand,'.'), strlen($filename)-1); // Get the extension from the filename. 
    Except the numbers ($rand) are appearing on the right side of the extension. How would I make it on the left of the extension?

  • #2
    Originally posted by Xaqa View Post
    I'm using this code:

    PHP Code:
       $file12 $_FILES['site_url']['name']; // Get the name of the file (including file extension).
       
    $file12 preg_replace('/\s/''_'$file12);
       
    $filename $file12.$rand;
       
    $ext substr($filenamestrpos($filename,$rand,'.'), strlen($filename)-1); // Get the extension from the filename. 
    Except the numbers ($rand) are appearing on the right side of the extension. How would I make it on the left of the extension?
    You've almost got it, just a bit of false logic I think.

    At the start, $file12 will contain the filename+extension. So, you want to create a filename from that, with a random number just before the extension. The extension will proceed after the last . in the filename. So, you would use strrpos() to find the last occurence of . which will guarantee the remainder of the string will be the extension.
    Using this produces the following:
    PHP Code:
        // Get the name of the file (including file extension).
        
    $file12 $_FILES['site_url']['name']; 

        
    $file12 preg_replace('/\s/''_'$file12);

        
    // Find extension position
        
    $position strrpos($file12'.');
        
    $filename substr($file12,0,$position).$rand.substr($file12,$position); 
    Tested it, and it works . This also allows for filenames that have more than 1 . as the last . in a filename will always preceed the extension.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

    Comment


    • #3
      Thank you for that it works. But the uploaded file has entirley different numbers. Here's the code:
      PHP Code:
         if(move_uploaded_file($_FILES['site_url']['tmp_name'],$upload_path $filename)) 

      Comment


      • #4
        Show me the whole script. You must be assigning $rand twice, and the filename twice, if the numbers are different. Can't tell without seeing the whole of your script
        Useful function to retrieve difference in times
        The best PHP resource
        A good PHP FAQ
        PLEASE remember to wrap your code in [PHP] tags.
        PHP Code:
        // Replace this
        if(isset($_POST['submitButton']))
        // With this
        if(!empty($_POST))
        // Then check for values/forms. Some IE versions don't send the submit button 
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

        Comment


        • #5
          Home File - http://pastebin.com/Xa7G52hg
          MySQL Upload - http://pastebin.com/WL7bEpH0

          Comment


          • #6
            Your problem is your mysql portion is re-processing the form submit data. It actually looks like you've just copied and pasted the same segments of code into both pages without thinking what it will actually do. rand() generates a random number, and using that to try and figure out the old filename is a pointless venture as rand() will generate a different number every time (in theory, lol).

            It looks like the editSite() is designed to parse form input, so I would remove the form processing from the home page, and leave the editSite() method in the mysql to do it's job there.
            Useful function to retrieve difference in times
            The best PHP resource
            A good PHP FAQ
            PLEASE remember to wrap your code in [PHP] tags.
            PHP Code:
            // Replace this
            if(isset($_POST['submitButton']))
            // With this
            if(!empty($_POST))
            // Then check for values/forms. Some IE versions don't send the submit button 
            Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

            Comment


            • #7
              Sorry, I'm not following you. Remove what part?

              Comment


              • #8
                Is this issue at all related to your other topic?
                ZCE

                Comment


                • #9
                  Your code is very messy. Did you write this? It's poor formatting is giving me a right headache haha. There's not a single { bracket surrounding long segments of if conditioned code, which isn't essential but makes everything SO much easier to understand and read.

                  Anyway, remove the image processing part from the home page (the bit down the bottom). It is causing you to process the images twice (i'm sure if you look at your directory, there's duplicates of your test images there) which is why the numbers aren't matching up.
                  Useful function to retrieve difference in times
                  The best PHP resource
                  A good PHP FAQ
                  PLEASE remember to wrap your code in [PHP] tags.
                  PHP Code:
                  // Replace this
                  if(isset($_POST['submitButton']))
                  // With this
                  if(!empty($_POST))
                  // Then check for values/forms. Some IE versions don't send the submit button 
                  Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

                  Comment


                  • #10
                    Originally posted by kbluhm View Post
                    Is this issue at all related to your other topic?
                    http://www.codingforum.net/showthread.php?t=237361
                    Looks like it was at first yeah. Looks like he never implemented your code properly. Regardless of double post, he's also implemented this code improperly as well and needs assistance with a newly created problem. You shouldn't double post in the future though, Xaqa.

                    Just a small critique on your code though kbluhm (hope you don't mind), if there's more than 1 . in the filename (which is unlikely, but can happen) then your script will cut off the extension, and maybe other information. My script takes the last . in the string, which is guaranteed to separate the extension only.
                    Useful function to retrieve difference in times
                    The best PHP resource
                    A good PHP FAQ
                    PLEASE remember to wrap your code in [PHP] tags.
                    PHP Code:
                    // Replace this
                    if(isset($_POST['submitButton']))
                    // With this
                    if(!empty($_POST))
                    // Then check for values/forms. Some IE versions don't send the submit button 
                    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

                    Comment


                    • #11
                      Originally posted by BluePanther View Post
                      Just a small critique on your code though kbluhm (hope you don't mind), if there's more than 1 . in the filename (which is unlikely, but can happen) then your script will cut off the extension, and maybe other information. My script takes the last . in the string, which is guaranteed to separate the extension only.
                      Incorrect, mine works just fine. You missed the third parameter for explode(). The `2` limits it to 2 pieces:
                      PHP Code:
                      $filename 'filename.tar.gz';

                      $pieces explode'.' $filename);

                      echo 
                      $pieces[0]; // filename
                      echo $pieces[1]; // tar.gz 
                      ZCE

                      Comment


                      • #12
                        I should reword what I said. Mine is more robust because with your code, the resultant output for some files would be unexpected - foo.bar.php would evaluate to foo.rand().bar.php whereas Xaqa's requirement is to have the random value to the left of the extension separator specifically. But I also see your point that if there was a file that was a tar.gz, the extension would become useless in that filename.tar.gz would become filename.tar.rand().gz. However, in Xaqa's example alone that won't be a problem as he is limiting uploads to single-extension images. For an unspecific file extension, yours would be better than mine but wouldn't be ideal if they were wanting some degree of filename preservation.
                        Useful function to retrieve difference in times
                        The best PHP resource
                        A good PHP FAQ
                        PLEASE remember to wrap your code in [PHP] tags.
                        PHP Code:
                        // Replace this
                        if(isset($_POST['submitButton']))
                        // With this
                        if(!empty($_POST))
                        // Then check for values/forms. Some IE versions don't send the submit button 
                        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

                        Comment


                        • #13
                          While not understand anything you both just said, I made this instead:

                          PHP Code:
                          <?php
                          $site_id 
                          intval($_GET['id']);
                          $site $sites->getSite($site_id);
                             
                          // Configuration - Your Options
                                
                          $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
                                
                          $max_filesize 524288// Maximum filesize in BYTES (currently 0.5MB).
                                
                          $upload_path './uploads/'// The place the files will be uploaded to (currently a 'files' directory).
                          $rand rand(1,99999999);
                              
                          $file12 $_FILES['site_url']['name'];  

                              
                          $file12 preg_replace('/\s/''_'$file12); 
                              
                          // Find extension position 
                              
                          $position strrpos($file12'.'); 
                              
                          $filename substr($file12,0,$position).$rand.substr($file12,$position);  
                           
                             
                          // Now check the filesize, if it is too large then DIE and inform the user.
                             
                          if(filesize($_FILES['site_url']['tmp_name']) > $max_filesize)
                                die(
                          'The file you attempted to upload is too large.');

                                                      if (
                          file_exists($filename))
                                                        die(
                          'File exsist.');

                          $con mysql_connect("crowngaming.ipagemysql.com","crowngaming","Hipporules2?");
                          if (!
                          $con)
                            {
                            die(
                          'Could not connect: ' mysql_error());
                            }

                          mysql_select_db("schematics"$con);

                          mysql_query("UPDATE sites SET site_url = '$filename' WHERE site_url = site_id");

                          mysql_close($con);
                           
                             
                          // Check if we can upload to the specified path, if not DIE and inform the user.
                             
                          if(!is_writable($upload_path))
                                die(
                          'You cannot upload to the specified directory, please CHMOD it to 777.');
                           
                             
                          // Upload the file to your specified path.
                             
                          if(move_uploaded_file($_FILES['site_url']['tmp_name'],$upload_path $filename))
                                            echo 
                          'Sucessfully uploaded file. <a href="/index.php?page=site_view&id=' .$row['site_id'] .'">Here</a>';
                          else
                           echo 
                          "Error!";
                          ?>
                          Which connect to MySQL databse and updates the site_url. But it updates for all the site_urls'. Can I make it so that it only updates for one?

                          Comment


                          • #14
                            I'm pretty sure that's what I was trying to get you to do. I'm not really sure what's not to get haha, you were processing the form twice - once on the home page and once in the mysql function.

                            Never the less, you're almost right but you're using the wrong information in the WHERE clause of your query. You're currently comparing two different fields. I noticed you retrieving the id from GET up the top, yet your form uses POST data. Not unusual, but how are you passing the id of the row currently being updated? However it is, your query should update "WHERE site_id=$id".
                            Useful function to retrieve difference in times
                            The best PHP resource
                            A good PHP FAQ
                            PLEASE remember to wrap your code in [PHP] tags.
                            PHP Code:
                            // Replace this
                            if(isset($_POST['submitButton']))
                            // With this
                            if(!empty($_POST))
                            // Then check for values/forms. Some IE versions don't send the submit button 
                            Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

                            Comment


                            • #15
                              Alright, I tried what you said replacing the WHERE with WHERE site_id=$id. Now what comes up is, nothing for site_url. Edit: It was '" . intval($site_id) . "' not $id :P
                              Last edited by Xaqa; Sep 11, 2011, 10:27 PM.

                              Comment

                              Working...
                              X
                              😀
                              🥰
                              🤢
                              😎
                              😡
                              👍
                              👎