Help:How to Check existing upload file name

Hi,
Can anyone help me in this script.i have just find this on forum.but i cant analyze where to check the existing upload filename…

this is my script…


<?php
session_start();
//var_dump($_POST);
include_once("conn.php");
$ran=rand();

while(list($key,$value) = each($_FILES["images"]["name"])){
if (($_FILES["images"]["size"][$key] < 200000))
{
	
	$filename = $value;
	$add = "upload/$filename";
	$imagename=($_FILES['images']['name'][$key]);
	if(!empty($value)) 
	{ 
	//echo $add;
	//echo "<br>".$_FILES[images][tmp_name][$key];
	//echo"<br>". $imagename;
	if(copy($_FILES[images][tmp_name][$key],$add)) 
	chmod("$add",0777);
	}
	
	$i = $i+1;
	$img[$i]=($_FILES['images']['name'][$key]);
	
}
}

$image1=trim($img[1]);
$image2=trim($img[2]);
$image3=trim($img[3]);

if(trim($_POST['desc'])<> ""){
header('location:viewticketuser.php');
$msg=addslashes($_POST['desc']);
//echo $msg;
insert(trim($_POST['cat']),$_POST['subcat'],trim($msg),trim($_SESSION['username']),trim($image1),trim($image2),trim($image3),trim($_SESSION['email']),trim($_SESSION['Dept']),trim($_POST['stat']),$con); 


}else{
echo'Enter a Description';

}

function insert($cat,$subcat,$message,$uname,$image1,$image2,$image3,$mail,$Dept,$Stat,$con)
{
mssql_query( "sp_ticket @cat='$cat',@subcat='$subcat',@desc='".str_replace("'","''",$message)."',@uname='$uname',@img1='$image1',@img2='$image2',@img3='$image3',@email='$mail',@dept='$Dept',@Stat='$Stat'");
		mssql_close($con);
}
?>

Thanks…

Your question is a bit vague, but I think you are looking for file_exists()

Hi…
Thanks for the reply…

What i mean is where to put the code to check if there is a existing value or data that has been upload by the previous user…

is it inside the while?

If you want to cancel the upload if a file name already exists, you could put the following code after the line that says $add = “upload/$filename”;

if(file_exists($add))
continue;

What this does is first check if the filename already exists, and if it does it goes to the next file.

Yup i tried it…

my idea is if there is a file existing i will just add a rand in the first line…but it doesn’t add up.


<?php
session_start();
//var_dump($_POST);
include_once("conn.php");
$ran=rand();

while(list($key,$value) = each($_FILES["images"]["name"])){
	if (($_FILES["images"]["size"][$key] < 200000))
	{
		
		$filename = $value;
		$add = "upload/$filename";

		$imagename=($_FILES['images']['name'][$key]);
		
		if(!empty($value)) 
		{ 
		if(file_exists($add)){
			 $add=$ran.$imagename."<br/>";
		}elseif(copy($_FILES[images][tmp_name][$key],$add)){
			chmod("$add",0777);
		}
		
		$i = $i+1;
		 $img[$i]=($_FILES['images']['name'][$key]);
		}
	}
}


 $image1=trim($img[1]);
 $image2=trim($img[2]);
 $image3=trim($img[3]);


$msg=addslashes($_POST['desc']);

insert(trim($_POST['cat']),$_POST['subcat'],trim($msg),trim($_SESSION['username']),trim($image1),trim($image2),trim($image3),trim($_SESSION['email']),trim($_SESSION['Dept']),trim($_POST['stat']),$con); 




function insert($cat,$subcat,$message,$uname,$image1,$image2,$image3,$mail,$Dept,$Stat,$con)
{
mssql_query( "sp_ticket @cat='$cat',@subcat='$subcat',@desc='".str_replace("'","''",$message)."',@uname='$uname',@img1='$image1',@img2='$image2',@img3='$image3',@email='$mail',@dept='$Dept',@Stat='$Stat'");
		mssql_close($con);
}


I don’t know what you mean with “it doesn’t add up” here, but I can see some mistakes.

First of all, where you set the new filename you forget to add the upload folder, so it will end up in the wrong place. Secondly for some reason you added a “<br/>” at the end of the file name.
Another thing is, it only checks if the file exists once now, if by chance it gets the same new random filename that already exists as well it will still override it.

Keeping in mind the above things, I would change this:


if(file_exists($add)){
     $add=$ran.$imagename."<br/>";
}elseif(copy($_FILES[images][tmp_name][$key],$add)){
     chmod("$add",0777);
}

To this:


while(file_exists($add)){
     $add = "upload/".rand().$filename;
}
if(copy($_FILES[images][tmp_name][$key],$add)){
     chmod("$add",0777);
}else{
     // error when trying to copy
}


$num = "";
while(file_exists($filename)) {
  $filename = explode('.',$filename);
  $filename[0] = substr($filename[0],0,(strlen($num)*-1));
  $num++;
  $filename[0] .= $num;
  implode('.',$filename);
}

(Added benefit: Objects are now sortable by age because they go sequentially)

edit: silly me. 0 case is special to substr.


$num = "";
while(file_exists($filename)) {
  $filename = explode('.',$filename); 
  if($num != "") {
  $filename[0] = substr($filename[0],0,(strlen($num)*-1));
  }
  $num++;
  $filename[0] .= $num;
  implode('.',$filename);
}  

Thanks…

one more question…sorry for the novice question…

how do i extract the filename without the “upload”…

Thanks…

Why would you need to “extract” the filename, it’s stored in $filename.
If you mean the filename with the new random number attached, simply change the script a little bit to this:


$original_name = $filename;

while(file_exists($add)){

     $filename = rand().$original_name;
     $add = "upload/".$filename;

}
if(copy($_FILES[images][tmp_name][$key],$add)){
     chmod("$add",0777);
}else{
     // error when trying to copy
}

And now your new filename is stored in $filename.

Thanks again…

it works fine now…
i just need to extract the file with no… to save it to my database…

Thanks again for you help really appreciate it…

If you are saving your filename to the database, I would recommend saving the path with it.
Now you probably have where you ouput the files something like this:

<img src=“upload/<?=$filename?>” />

However if you put the filename in the database with the path “upload/” in it, you could just do

<img src=“<?=$filename?>” />

and then if you ever change the path in the upload script it will still work.

Actually i would recommend exactly the opposite.

#1: You’re storing redundant information, inflating your database.
#2: If you do decide to change /upload/ to /uploadedfiles/ later on, you’ll have to correct every single database entry, whereas with it coded in, you can just alter a single file with 7 characters and reupload it.

I guess this highly depends on the system, what I meant by ‘changing the path’ was for instance a second upload page which does the same but saves to a different location. So you would have the database filled with both images from /upload/ and /uploadedfiles/.

If you would change the folder alltogether it would be unwise to do it my way yeah.

it’s still redundant data - if you’re going to do multiple folders, design them in such a way that you dont have to store the whole filename over and over. (If, for example, you were going to store them on a per-user basis, you wouldnt need to store /<username>/file.jpg, because your script knows <username> anyway.)

If you’re going to let users create their own directories and such, then you may need to let them put full paths in.