leitning_strike Posted January 17, 2011 Share Posted January 17, 2011 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 More sharing options...
Muddy_Funster Posted January 17, 2011 Share Posted January 17, 2011 Have you tried a print_r() on the $userfile['error'] to see what the value is? Link to comment https://forums.phpfreaks.com/topic/224745-avoiding-duplicate-file-names/#findComment-1160895 Share on other sites More sharing options...
Maq Posted January 17, 2011 Share Posted January 17, 2011 You only enter the loop when $dup = true so there must not be a duplicate. Link to comment https://forums.phpfreaks.com/topic/224745-avoiding-duplicate-file-names/#findComment-1160897 Share on other sites More sharing options...
mikosiko Posted January 17, 2011 Share Posted January 17, 2011 // 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. Link to comment https://forums.phpfreaks.com/topic/224745-avoiding-duplicate-file-names/#findComment-1160898 Share on other sites More sharing options...
leitning_strike Posted January 17, 2011 Author Share Posted January 17, 2011 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 Link to comment https://forums.phpfreaks.com/topic/224745-avoiding-duplicate-file-names/#findComment-1160903 Share on other sites More sharing options...
leitning_strike Posted January 17, 2011 Author Share Posted January 17, 2011 Ah I'm a bleeping idiot (or I've been up way too long). I forgot the single quotes around $name in the query Link to comment https://forums.phpfreaks.com/topic/224745-avoiding-duplicate-file-names/#findComment-1160905 Share on other sites More sharing options...
mikosiko Posted January 17, 2011 Share Posted January 17, 2011 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 Link to comment https://forums.phpfreaks.com/topic/224745-avoiding-duplicate-file-names/#findComment-1160907 Share on other sites More sharing options...
leitning_strike Posted January 17, 2011 Author Share Posted January 17, 2011 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 Link to comment https://forums.phpfreaks.com/topic/224745-avoiding-duplicate-file-names/#findComment-1160922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.