If File Exists trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

If File Exists trouble

Post by Smudly »

I am working on a section for users to upload a picture/avatar on their profile. I noticed that if I have two different images with the same file name, and upload both of them, the first one is overwritten. I am trying to write a script that will find if file exists, randomize a set of numbers and letters, and use those as the files' new name.

In my code below, you can see that I am trying to replace the $name variable with this new set of characters. The problem is, that the name has the extension at the end of it as well. So this would cause an issue. So depending on what type of file the user is trying to submit, the extension will be concatenated at the end of the file name, then will upload it.

I'm getting no errors, and am stuck on what to do from here. The function creates a random string of characters for the name.

Code: Select all

<?php

$upload = $_POST['upload'];
if ($upload)
{
	// Name of file
$name = $_FILES["image"]["name"];
	// Type of file (video/avi) or image/jpg, etc
$type = $_FILES["image"]["type"];
	//size of file
$size = $_FILES["image"]["size"];
	//stores file in a temporary location
$temp = $_FILES["image"]["tmp_name"];
	// if there is an error
$error = $_FILES["image"]["error"];
echo $type;
function genRandomString() {
    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}

if ($error > 0)
{
	$uploaderror = "An error occured. Please try again.";
}
else
{

if ($type=="image/jpeg" || $type=="image/png" || $type=="image/gif" || $type=="image/bmp" || $type=="image/jpeg")
{
	if ($size <= 5242880)
	{
		if(file_exists($name)){
		
		$name = genRandomString($string);
		
		 
		}
		else
		{
			move_uploaded_file($temp, "avatars/".$name);
			$success = "Upload Complete!";
		}
	}
	else{
		$uploaderror = "Your image must be less than 5 megabytes.";
	}
}
else
{
die("That format is not allowed!");

}
}
}
?>
<html>
<h1>Upload a File</h1>
<form action="upload.php" method="POST" enctype="multipart/form-data">
	<input type="file" name="image"><br />
	<input type="submit" name="upload" value="Upload"><br />
	<?php echo $success, $uploaderror; ?>

</form>
</html>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: If File Exists trouble

Post by social_experiment »

You could write a function that returns the extension of the file :

Code: Select all

<?php
function getExtension($value) {
 $clean_value = trim($value);
 $file_extension = substr($clean_value, -4);
 return $file_extension
} ?>
Then when you upload the file add the extension :

Code: Select all

<?php
// other code
if(file_exists($name)){   
               //pass the file name to the function which returns the extension of 
               //said file
                $file_extension = getExtension($name);             
                $file_name = genRandomString();            
                $name = $file_name.$file_extension;
} ?>

Code: Select all

<?php $name = genRandomString($string); ?>
What is the value of '$string' in this instance? You don't have to pass anything to this function, the value of '$string' is equated to '$name' without an argument.

Code: Select all

<?php ($type=="image/jpeg" || $type=="image/png" || $type=="image/gif" || $type=="image/bmp" || $type=="image/jpeg") ?>
This isn't very effective to stop users from only upload image files and can be bypassed.
viewtopic.php?f=50&t=102106
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply