#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Help with uploading images


    Hi, I have a script that sends uploaded images to my images folder on my web server. Im not sure but I think it also renames the images (which is what I want to happen). Could someone please take a look at this script for me and tell me if it does rename the images?

    I also want the script to send the name of the image, after it's renamed, to the database table in row labeled 'images'. Could someone help me with this?

    Heres the script:
    PHP Code:
    <?php
    // define a constant for the maximum upload size
    define ('MAX_FILE_SIZE'1024 50);
    if (
    array_key_exists('upload'$_POST)) {
      
    // define constant for upload folder
      
    define('UPLOAD_DIR''/path/to/images/');
      
    // replace any spaces in original filename with underscores
      
    $file str_replace(' ''_'$_FILES['image']['name']);
      
    // create an array of permitted MIME types
      
    $permitted = array('image/gif''image/jpeg''image/pjpeg',
    'image/png');
      
    // upload if file is OK
      
    if (in_array($_FILES['image']['type'], $permitted)
              && 
    $_FILES['image']['size'] > 
              
    && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
            switch(
    $_FILES['image']['error']) {
              case 
    0:
                    
    // check if a file of the same name has been uploaded
                    
    if (!file_exists(UPLOAD_DIR $file)) {
                      
    // move the file to the upload folder and rename it
                      
    $success =
    move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .
    $file);
                    } else {
                      
    $result 'A file of the same name already exists.';
                    }
                    if (
    $success) {
                      
    $result "$file uploaded successfully.";
                    } else {
                      
    $result "Error uploading $file. Please try again.";
                    }
                    break;
              case 
    3:
              case 
    6:
              case 
    7:
              case 
    8:
                    
    $result "Error uploading $file. Please try again.";
                    break;
              case 
    4:
                    
    $result "You didn't select a file to be uploaded.";
            }
      } else {
            
    $result "$file is either too big or not an image.";
      }
    }
    ?>
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Your upload script is not secure at all because you do not validate the file extension.

    It may or may not rename the file, depending on the file's original name, but if you want it to rename the file then presumably you already have some scheme in mind for renaming the file?
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    8
    Validate the file extension first, there are a number of ways to do this in PHP.

    Next, just run a custom function something like edit_filename(){}

    You do not need to pass in the value of your file name because superglobals are available to the whole script including functions, without the need to pass the value in via a parameter.

    There are some handy string functions which allow you to cut the extension off the filename string, store it somewhere, you can then generate a random 2 digit number for example, concatenate that to the end of the filename, then concatenate the extension value back onto the filename to give you the 'edited' filename. I've currently wrote something exactly like this for my current site and it works good.

    Kind regards,

    NM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Pakistan
    Posts
    3
    Rep Power
    0
    Originally Posted by LukePVB
    Hi, I have a script that sends uploaded images to my images folder on my web server. Im not sure but I think it also renames the images (which is what I want to happen). Could someone please take a look at this script for me and tell me if it does rename the images?

    I also want the script to send the name of the image, after it's renamed, to the database table in row labeled 'images'. Could someone help me with this?

    Heres the script:
    PHP Code:
    <?php
    // define a constant for the maximum upload size
    define ('MAX_FILE_SIZE'1024 50);
    if (
    array_key_exists('upload'$_POST)) {
      
    // define constant for upload folder
      
    define('UPLOAD_DIR''/path/to/images/');
      
    // replace any spaces in original filename with underscores
      
    $file str_replace(' ''_'$_FILES['image']['name']);
      
    // create an array of permitted MIME types
      
    $permitted = array('image/gif''image/jpeg''image/pjpeg',
    'image/png');
      
    // upload if file is OK
      
    if (in_array($_FILES['image']['type'], $permitted)
              && 
    $_FILES['image']['size'] > 
              
    && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
            switch(
    $_FILES['image']['error']) {
              case 
    0:
                    
    // check if a file of the same name has been uploaded
                    
    if (!file_exists(UPLOAD_DIR $file)) {
                      
    // move the file to the upload folder and rename it
                      
    $success =
    move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .
    $file);
                    } else {
                      
    $result 'A file of the same name already exists.';
                    }
                    if (
    $success) {
                      
    $result "$file uploaded successfully.";
                    } else {
                      
    $result "Error uploading $file. Please try again.";
                    }
                    break;
              case 
    3:
              case 
    6:
              case 
    7:
              case 
    8:
                    
    $result "Error uploading $file. Please try again.";
                    break;
              case 
    4:
                    
    $result "You didn't select a file to be uploaded.";
            }
      } else {
            
    $result "$file is either too big or not an image.";
      }
    }
    ?>
    Just use Two Variables With $OriginalName and $NewName, and just put this in your original name variable as $originalName = $_FILES['FileField']['name']; and Just right after this you need to change this with rename() function available in PHP. and save that with your $newName and then you can insert both values into your database as INSERT INTO tablename (OriginalName, NewName) VALUES ($originalName, $newName);
    And in the same case you will upload the file into your directory with new name move_uploaded_file($newName and Your destination folder here);

IMN logo majestic logo threadwatch logo seochat tools logo