Help with tweaking my image upload

Hello there, my name is Vinny.

I’m gonna try and be as straightforward as possible.

What I want to do is, when I upload an image, it automatically renames it to “Example 1”, but if “Example 1” already exists, it renames it to “Example 2”, and if “Example 2” exists, it renames it to “Example 3”, so on and so forth.

I’ve little knowledge in PHP, but I have this base script that stores uploaded images in my predefined location.

[php]<?php
$target_path = “images/cars/”;

$target_path = $target_path . basename( $_FILES[‘uploadedfile’][‘name’]);

if(move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path)) {
echo "The photo(s) “. basename( $_FILES[‘uploadedfile’][‘name’]).
" has been uploaded”;
} else{
echo “There was an error uploading the photo(s), please try again!”;
}
?>[/php]

You would use the file_exists command…

http://www.php.net/file_exists

Something like this should work, please not this is off the top of my head and my contain errors.

[php]<?php

$target_path = “images/cars/example_”;
$i = 0;
do {
$i++;
} while(file_exists($target_path . $i . “.jpg”));

$target_path = $target_path . $i . “.jpg”;

if(move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path)) {
echo "The photo(s) “. basename( $_FILES[‘uploadedfile’][‘name’]).
" has been uploaded”;
} else{
echo “There was an error uploading the photo(s), please try again!”;
}
?>[/php]

Just to add, you could change the file name or amend it to contain something unique. Something like ddmmyyhhmm to make your file name look like:

image1201220131250.jpg
Sponsor our Newsletter | Privacy Policy | Terms of Service