Jump to content

duplicate file names upload script


ciscodude

Recommended Posts

Hi all,

 

This is my very first post here, so I will try and make it count  ;)

 

Very recently I have been working with php and I found this simple little script that allows users to upload files. I tweaked some things to only make it upload .doc files but now I have a slight problem.

Every time someone uploads a word file with the same name, it gets replaced.

 

I tried looking for other examples here on the forum, but was unable to apply it to my own script.

 

It would be a big help if someone could provide a simple add-on to this existing script.

 

Thanks in advance.

 

Script:

 

<?

 

$locatie="upload/";

$toegestaan = "doc";

$max_size = 1000000;

 

 

set_time_limit(0);

 

if(isset($_POST['upload']))

{

    if(is_uploaded_file($_FILES['bestand']['tmp_name']))

    {

        $extensie_bestand = pathinfo($_FILES['bestand']['name']);

        $extensie_bestand = $extensie_bestand[extension];

 

        $extensies_toegestaan = explode(", ", $toegestaan);

 

        for($i = 0; $i < count($extensies_toegestaan); $i++)

        {

            if($extensies_toegestaan[$i] == "$extensie_bestand")

            {

                $ok = 1;

            }

        }

 

        if($ok == 1)

        {

            if($_FILES['bestand']['size']>$max_size)

            {

                echo "Het bestand is te groot, de maximale grootte is: <b>$max_size</b>";

                exit;

            }

 

            if(!move_uploaded_file($_FILES['bestand']['tmp_name'],

            $locatie.$_FILES['bestand']['name']))

            {

                echo "het bestand kan niet worden verplaatst";

                exit;

            }

 

            echo "Het bestand ".$_FILES['bestand']['name']." is geupload<br>

            <a href='".$locatie."".$_FILES['bestand']['name']."' </a>";

        }

        else

        {

            echo "Verkeerde extentie, de toegestane extensies zijn: <b>$toegestaan</b>";

        }

    }

    else

    {

        echo "Het uploaden is mislukt, probeer het opnieuw";

    }

 

}

 

?>

<title>test tittle</title>

<style type="text/css">

<!--

body {

margin-top: 0px;

}

-->

</style></head>

 

<body>

<table width="1216" height="1191" border="0" cellpadding="0" cellspacing="0" background="back">

  <tr>

    <td height="317" colspan="2"> </td>

  </tr>

  <tr>

    <td height="381"> </td>

    <td><p> </p>

      <form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data">

        <table width="398" border="0" cellspacing="0" cellpadding="0">

          <tr>

            <td width="180"> </td>

            <td width="218"><input type="file" name="bestand" />

                <input type="submit" name="upload" value="uploaden" /></td>

          </tr>

          <tr>

            <td> </td>

            <td> </td>

          </tr>

        </table>

      </form></td>

  </tr>

  <tr>

    <td width="420"> </td>

    <td width="796"> </td>

  </tr>

</table>

Link to comment
Share on other sites

Perhaps something like this...

 

<?PHP
$file_name = "somefile.doc";
$path_to_save = "/docs";
$ok = 0;
if(file_exists($path_to_save ."/". $file_name)) { $ok = 1; }
while($ok==1) {
$prefix = date("Ymdhis");
if(!file_exists($path . "/" . $prefix . $file_name)) {
	$file_name = $prefix . $file_name;
	$ok = 0;
}
}
?>

Link to comment
Share on other sites

I tried adding it before, after, between the script, but my scripting knowledge is just to bad.

 

If I understand correct the duplicate doc file will be moved to the docs folder..?

Are there any variables I need to change..?

Also I don't get the $file_name = "somefile.doc"; what function does it have..?

 

Sorry for the uber noobish questions...  :shrug:

Link to comment
Share on other sites

The file names people will be uploading are variable. I don't know in advance what they will be. maybe someone will enter joe.doc and another joe comes and uploads joe.doc to. it could also be something else like mary.doc.

Am I missing something here or is this what you are referring to..?

 

Also I can't pinpoint the area within the script that will be saving/moving the file, where would that be..?

 

Thanks in advance.

 

 

Link to comment
Share on other sites

Ok here is example untested and un-proof-read; however I think you will get the gist of it...

 

<?PHP
if(isset($_POST['uploadedfile'] {
$allowed_ext = "doc";
$path_to_save = "uploads/";
$file_name = basename( $_FILES['uploadedfile']['name']);
$parts = pathinfo($file_name);
$ext = $parts ['extension'];
if($allowed_ext != strtolower($ext)) { echo "Invalid file type - Try again"; exit(); }
$ok = 0;
if(file_exists($path_to_save . $file_name)) { $ok = 1; }
	while($ok==1) {
		$prefix = date("Ymdhis");
		if(!file_exists($path_to_save . $prefix . $file_name)) {
			$file_name = $prefix . $file_name;
			$ok = 0;
		}
}
$target = $path_to_save . $file_name; 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) {
	if($file_name ==  basename( $_FILES['uploadedfile']['name'])) {
		echo "The file ".  basename( $_FILES['uploadedfile']['name']) . " has been uploaded ";
	}else{
		echo "The file ".  basename( $_FILES['uploadedfile']['name']) . " has been uploaded  and renamed " . $file_name;
	}
} else{
    echo "There was an error uploading the file, please try again!";
}
}else{
?>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
<?PHP
}
?>

Link to comment
Share on other sites

Try this, I added a timestamp function and placed it in your code.

 

<?php
function addTimestamp($filename) {
$character = "/";
$target_check = strrchr($filename,$character);
    if ($target_check[$character]) {
     $temp_filename1 = end(explode($target_check[$character],$filename));
     $target = true;
     } else {
      $temp_filename1 = $filename;
      }   
$temp_filename = strtolower(trim($temp_filename1));
$timestamp = date('Y-m-d-H-i-s');
if ($target) {
$new_filename = str_replace($temp_filename1, "$timestamp-$temp_filename", $filename);
} else {
$new_filename = "$timestamp-$temp_filename";
}
return $new_filename;
}

$locatie="upload/";
$toegestaan = "doc";
$max_size = 1000000;


set_time_limit(0);

if(isset($_POST['upload']))
{
    if(is_uploaded_file($_FILES['bestand']['tmp_name']))
    {
        $extensie_bestand = pathinfo($_FILES['bestand']['name']);
        $extensie_bestand = $extensie_bestand[extension];

        $extensies_toegestaan = explode(", ", $toegestaan);

        for($i = 0; $i < count($extensies_toegestaan); $i++)
        {
            if($extensies_toegestaan[$i] == "$extensie_bestand")
            {
                $ok = 1;
            }
        }

        if($ok == 1)
        {
            if($_FILES['bestand']['size']>$max_size)
            {
                echo "Het bestand is te groot, de maximale grootte is: <b>$max_size</b>";
                exit;
            }

            if(!move_uploaded_file($_FILES['bestand']['tmp_name'],
            $locatie.$_FILES['bestand']['name']))
            {
                echo "het bestand kan niet worden verplaatst";
                exit;
            }
            $filename = addTimestamp($locatie.$_FILES['bestand']['name']);
           echo "Het bestand ".$_FILES['bestand']['name']." is geupload<br>
            <a href='$filename'>$filename</a>";
        }
        else
        {
            echo "Verkeerde extentie, de toegestane extensies zijn: <b>$toegestaan</b>";
        }
    }
    else
    {
        echo "Het uploaden is mislukt, probeer het opnieuw";
    }

}

?>
<title>test tittle</title>
<style type="text/css">
<!--
body {
   margin-top: 0px;
}
-->
</style></head>

<body>
<table width="1216" height="1191" border="0" cellpadding="0" cellspacing="0" background="back">
  <tr>
    <td height="317" colspan="2"> </td>
  </tr>
  <tr>
    <td height="381"> </td>
    <td><p> </p>
      <form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
        <table width="398" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="180"> </td>
            <td width="218"><input type="file" name="bestand" />
                <input type="submit" name="upload" value="uploaden" /></td>
          </tr>
          <tr>
            <td> </td>
            <td> </td>
          </tr>
        </table>
      </form></td>
  </tr>
  <tr>
    <td width="420"> </td>
    <td width="796"> </td>
  </tr>
</table>

 

But litebearer's code is much better for the upload

Link to comment
Share on other sites

I tried the code you provided quickoldcar, and it seems to work fine and even tells me that the file was successfully uploaded (with the timestamp), but in reality it just saves it under the same name without the timestamp...

 

I  also think litebearer code is much better, but I can't keep up with him. To little knowledge of programming here. At this point I am the copy/paste guy who is able to change some variables to make it work the way I want it.

 

I tried adding )) at the end of the line: if(isset($_POST['uploadedfile']. It seems to work then (no error), but when I upload nothing happens...

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.