Jump to content

Upload file - change name if file has same name


jjmusicpro

Recommended Posts

Right now i have an uploading script working, a basic one.

However, if someone uploads a file with the same name, it will overwirte the previous one.

 

How can i make it so it checks to see if there is a file with the same name, if so, just put a number on the end that increments or something.?

 

Thanks for your help

 

$target_path = "uploads/";

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

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded.<br>";
echo "<img src=\"/uploads/".basename( $_FILES['uploadedfile']['name'])."\">";
} else{
    echo "There was an error uploading the file, please try again!";
}

Link to comment
Share on other sites

i dont know how to reloop it back through, and keep checking if that file name already exsists.

so if there is already a test.gif, it would go to test.gif1, however, if there is already a test.gif1..i need it to go to the next number.

or cant i add some weird time hash on the end of the file name?

Link to comment
Share on other sites

like this... (i have not tried executing this code.. )

 

<?php

$target_path = "uploads/";

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

$i = 0;
while(file_exists($target_path).$i) {
$i++;
}

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.$i)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded.<br>";
echo "<img src=\"/uploads/".basename( $_FILES['uploadedfile']['name'])."\">";
} else{
    echo "There was an error uploading the file, please try again!";
}

?>

Link to comment
Share on other sites

so this is what i have but its not changing name

 



<?
$target_path = "uploads/";

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

$i = 0;
while(file_exists($target_path.$i)) {
$i++;
}

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.$i)) {
echo "<img src=\"/uploads/".basename( $_FILES['uploadedfile']['name'])."\"><br><br>";
echo "<b>Direct Link</b><br>";
echo "<textarea cols=\"50\" rows=\"3\">http://www.blintool.com/uploads/".basename( $_FILES['uploadedfile']['name'])."</textarea>";
echo "<br><br>";
echo "<b>Myspace / FaceBook / Hi5 / Orkut Links</b><br>";
echo "<textarea cols=\"50\" rows=\"3\"><a href=\"http://www.blingtool.com\"><img src=\"http://www.blingtool.com/uploads/".basename( $_FILES['uploadedfile']['name'])."></a></textarea>";
} else{
    echo "There was an error uploading the file, please try again!";
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.