...

View Full Version : File upload, if file exists then add a number?!



jarv
11-11-2011, 02:57 PM
Hi,

I am doing a website with file upload facility and thought what if someone uploads a file with the same name as another?!

maybe a number can be added after it ie;

pic.jpg

pic.jpg already exists so change to: pic1.jpg

if pic1.jpg already exists the change to pic2.jpg and so on?!

here is my code so far:



<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("gallery/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"gallery/" . $_FILES["file"]["name"]);
echo "Stored in: " . "gallery/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

tangoforce
11-11-2011, 03:01 PM
Ideally you should rename the file using uniqid() OR just modify it to contain iteg:

picture.jpg becomes sdf987987-picture.jpg (or whatever uniqid() outputs.

jarv
11-11-2011, 03:54 PM
sorry, I'm not really following here?

do I do this then?


uniqid($_FILES["file"]["name"]);


what is iteg?

tangoforce
11-11-2011, 03:59 PM
$FileName = uniquid() .'-' .$_FILES['file']['name'];

jarv
11-11-2011, 04:21 PM
now I get the error:

Fatal error: Call to undefined function uniquid() in D:\i-spie.com\wwwroot\upload_file.php on line 8

here is my code:



<?php
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: index.php");
}
mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
$FileName = uniquid() .'-' .$_FILES['file']['name'];
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $FileName . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("gallery/" . $FileName))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"gallery/" . $FileName);

// Define $myusername and $mypassword
$UserID=$_POST['UserID'];
$PictureName=$_POST['PictureName'];
$PictureDate=$_POST['PictureDate'];
$TargetID=$_POST['TargetID'];

// To protect MySQL injection (more detail about MySQL injection)
$UserID = stripslashes($UserID);
$PictureName = stripslashes($PictureName);
$PictureDate = stripslashes($PictureDate);
$TargetID = stripslashes($TargetID);
$UserID = mysql_real_escape_string($UserID);
$PictureName = mysql_real_escape_string($PictureName);
$PictureDate = mysql_real_escape_string($PictureDate);
$TargetID = mysql_real_escape_string($TargetID);


$sql = "INSERT INTO rstargetpictures (UserID, FileName, PictureName, PictureDate, TargetID)
VALUES ('$UserID', '$FileName', '$PictureName', '$PictureDate', '$TargetID')";
$msg = "New booking added";
$result = mysql_query($sql) or die('Error: ' . mysql_error() . '<br>SQL: ' . $sql);

echo "Stored in: " . "gallery/" . $FileName;
}
}
}
else
{
echo "Invalid file";
}
?>

kbluhm
11-11-2011, 04:26 PM
Please please please try debugging for yourself, the PHP site is exceptionally helpful. tangoforce just typoed the function name is all:
http://www.php.net/uniqid

jarv
11-11-2011, 04:30 PM
ah yeah, thanks

coolcool
11-11-2011, 04:39 PM
try this


$FileCounter = 1;
while (file_exists( 'gallery/'.$file_name ))
$file_name = $FileCounter++.'_'.$file_name;

tangoforce
11-11-2011, 04:57 PM
Please please please try debugging for yourself

I second that. There is nothing better than beating the computer :cool:

Seriously though jarv, for the last few months you don't seem to have learned anything despite all of our help. Are you actually trying to learn or just pick up snippets of code and mix them all together?

PS sorry for the typo there but as kbluhm says a couple of seconds on php.net would of detected that along with looking at my previous ways of spelling it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum