Upload

Tags:    php

Hej er der en måde at få det her script til ikke at overskrive et billede på serveren hvis det har samme navn som det man uploader, jeg mener hvis der allerede ligger et billede kaldet "bil" i mappen photo på serveren og jeg uploader et andet billede også kaldet "bil" til mappen.

Hvis ikke kan man så så lave en funktion "tilføjelse i script" der fortæller uploader at der allerede er et billede på serveren med det navn.

Takker for hjælpen...

upload script

<html>
<head>
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
border: 1px solid #000000;
}
-->
</style>
</head>

<body>
<?
// you can change this to any directory you want
// as long as php can write to it
$uploadDir = '../photo/';


if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

// the files will be saved in filePath
$filePath = $uploadDir . $fileName;

// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

include 'library/config.php';
include 'library/opendb.php';

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

include 'library/closedb.php';

echo "<br>File uploaded<br>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>



5 svar postet i denne tråd vises herunder
1 indlæg har modtaget i alt 1 karma
Sorter efter stemmer Sorter efter dato
I din kode står der
Fold kodeboks ind/udKode 


Så den tjekker om filen eksisterer, men lige meget hvad overskriver den filen.

Det skal rettes til det her

Fold kodeboks ind/udKode 




Du kan jo tjekke om filen eksisterer inden du uploader den.
Fold kodeboks ind/udKode 




Indlæg senest redigeret d. 06.10.2006 06:52 af Bruger #3275
Hej

Hvor sætter jeg den kode ind i scriptet jeg er helt totalt nybegynder indenfor php
:P



Fold kodeboks ind/udKode 




Hej der var jeg lidt hurtig "jeg gør sådan men det virker ikke"


<html>
<head>
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
border: 1px solid #000000;
}
-->
</style>
</head>

<body>
<?
// you can change this to any directory you want
// as long as php can write to it
$uploadDir = '../photo/';


if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

// the files will be saved in filePath
$filePath = $uploadDir . $fileName;

// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
if(file_exists($filepath.$filename))
{
echo "Filen eksisterer allerede! Omdøb filen.";
exit;
}
else
{
}
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}


include 'library/config.php';
include 'library/opendb.php';

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

include 'library/closedb.php';

echo "<br>File uploaded<br>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>




t