Jump to content

Avoiding duplicate file names


leitning_strike

Recommended Posts

I've got a script where the client can upload pictures. The pictures are then resized, thumbnailed, and added to the database. In the process I'm trying to search the database for a duplicate file name and create a new name if necessary:

 

$userfile = 'userfile' . $i;
				$tmpLoc = $_FILES[$userfile]['tmp_name'];
				$name = $_FILES[$userfile]['name'];
				$error = $_FILES[$userfile]['error'];
				$type = $_FILES[$userfile]['type'];
				$temp = 'album' . $i;
				$album = $_POST[$temp];

				if($error > 0) {
					echo "Error on $name: ";
					switch ($error) {
					case 1: echo "File exceeded upload_max_filesize"; break;
					case 2: echo "File exceeded max_file_size"; break;
					case 3: echo 'File only partially uploaded'; break;
					case 4: echo 'No file uploaded'; break;
					}
					echo "</div>";
					exit;
				}       

				// Check for name duplicates and deal with
				$query = "SELECT * FROM pictures WHERE src = $name";
				$result = mysql_query($query);
				if($result)
					$dup = true;
				while($dup) {
                                                echo "Duplicate file name $name <br />";
					$ext;
					if($type == 'image/gif')
						$ext = '.gif';
					else if($type == 'image/jpeg')
						$ext = '.jpg';
					else if($type == 'image/png')
						$ext = '.png';
					else
						die("Error: Unsupported file type");

					$x = 0;
					$name = $x . $ext;
                                                echo "Checking $name <br />";
					$query = "SELECT * FROM pictures WHERE src = $name";
					$result = mysql_query($query);
					if(!$result) {
						$dup = false;
						echo "File successfully renamed to $name to avoid duplicate <br />";
					}

					$x++;
				}

 

I don't get any errors of any sort, it just never enters the loop

Link to comment
https://forums.phpfreaks.com/topic/224745-avoiding-duplicate-file-names/
Share on other sites

					// Check for name duplicates and deal with
				$query = "SELECT * FROM pictures WHERE src = $name";
				$result = mysql_query($query);
				if($result)
					$dup = true;
				while($dup) {

 

^^ seems like a wrong logic to me... try :

 

					// Check for name duplicates and deal with
				$query = "SELECT * FROM pictures WHERE src = $name";
				$result = mysql_query($query);
				if(mysql_num_rows($result) > 0)
					$dup = true;
				while($dup) {

 

From the manual regarding mysql_query():

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

RE Muddy Funster: There's no error on the upload, it proceeds to resize, thumbnail and add to the database, but it does so as a duplicate file name

 

RE Maq: Obviously I'm testing it with a duplicate file name

 

RE mikosiko: I tried that but I get a 'supplied argument is not a valid mysql resource' warning and still no loop

RE mikosiko: I tried that but I get a 'supplied argument is not a valid mysql resource' warning and still no loop

 

that is the right code... if you are getting that error then you have a mistake in a different place... maybe spaces/special characters in the $name variable?... use it enclosed in '

 

EDIT: I just read that you figure it out... good

RE mikosiko: I tried that but I get a 'supplied argument is not a valid mysql resource' warning and still no loop

 

that is the right code... if you are getting that error then you have a mistake in a different place... maybe spaces/special characters in the $name variable?... use it enclosed in '

 

EDIT: I just read that you figure it out... good

 

yeah, thanks though  :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.