PDA

View Full Version : An image upload Situation



npsari
04-22-2007, 04:10 PM
I am using this upload file script:


<?
// Where the file is going to be placed

$target_path = "images/";

/* Add the original filename to our target path. Result is "uploads/filename.extension" */

$target_path = $target_path . basename( $_FILES['Image1']['name']);
$_FILES['Image1']['tmp_name'];

$target_path = "images/";

$target_path = $target_path . basename( $_FILES['Image1']['name']);

if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) {

echo "The file ". basename( $_FILES['Image1']['name'])." has been uploaded";

} else{

echo "There was an error uploading the file, please try again!";
}

?>

But everytime someone uploads the same image name, the image is over-written.

So this is a little problem because users might upload same image names

How can i prevent the script from overwritting old images

boxxertrumps
04-22-2007, 04:55 PM
$target_path = "images/";
if (file_exists($targetpath)) {
$target_path = $target_path . "a" . basename( $_FILES['Image1']['name']);
} else {
$target_path = $target_path . basename( $_FILES['Image1']['name']);
}
if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) {

echo "The file ". basename( $_FILES['Image1']['name'])." has been uploaded";
rand()
} else{

echo "There was an error uploading the file, please try again!";
}

?>
Changes in red.

Adds an "a" infront of the filename, so if you want to upload file.jpg and it already exists, you have afile.jpg

jr_yeo
04-27-2007, 02:34 PM
or go to this (http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html) site