Jump to content

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  :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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