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
// 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'] > 0 
          && $_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.";
  }
}
?>

Recommended Answers

All 2 Replies

Member Avatar for LastMitch

Could someone please take a look at this script for me and tell me if it does rename the images?

Did you write this code?

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?

I mean the way you phrase your comments it seems like you are asking someone to modify the code?

I mean this the 4th time I answer your threads and I notice couple members also answer your thread more than once.

Each time it's the same thing you asking people to for advice then you ask that members or another members to modify it for you?

It's not right. You can't keep doing something like this.

People will notice it and most likely those people will ignore your threads.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.