eZe616 Posted May 28, 2007 Share Posted May 28, 2007 I have this script that displays the number of input fields depending on the number of images already uploaded to a certain ID. It's a multi-upload script. Now when I pass the variables through the process.php file, I'm trying for it to look into the database and directory, and see if a file with that name already exist. and If the files with that certain name exists it'll add +1 to the [key] value that has been passed, for each of the uploaded images. My images are named, depending on it's upload [key], and the ID so f.e. : $name = "HID".$id."_img".$nkey.".jpg"; where $nkey = $key +1; here's my scripts: <?php include 'dbcon.php'; $id = $_POST['id']; $sql = "SELECT * FROM house WHERE id='".$id."'"; $result = mysql_query($sql) or die("Could not connect"); $row = mysql_fetch_array($result); echo "<p>The following pictures have been uploaded to the profile of <b>".$row['name']." ".$row['lastn']. "</b></p>"; foreach ( $_FILES["pictures"]["error"] as $key => $error ) { if ( $error == UPLOAD_ERR_OK ) { $img = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; $path = "upload/$name"; // Giving the uploaded picture a new name based on ID and $key number $epl = explode(".", $name); $nkey = $key + 1; $nname = "HID".$id."_img".$nkey.".".$epl[1]; // Resizing the image $src = imagecreatefromjpeg($img); $width = imagesx($src); $height= imagesy($src); $x = 600; $y = ($height/$width) * 600; $dst = imagecreatetruecolor($x,$y); imagecopyresampled($dst,$src,0,0,0,0,$x,$y,$width,$height); // Retrieve data to see if a file with that name already exists /* Now this is a far as I can get without getting confused all in the script */ sql2 = "SELECT * FROM hpics WHERE id='".$id."'"; $result2 = mysql_query($sql2) or die("Error while retreiving picture information."); $row = mysql_fetch_array($result2); $ex_pic = $row['p1']; for ($i=1; $i <= 9; $i++ ) { $nkey = $key + 1; } $nname = "HID".$id."_img".$nkey.".".$epl[1]; /* End confusing part =========================================================================*/ $file = "upload/$nname"; imagejpeg($dst,$file); echo "<img src=\"".$file."\"><br /><br />"; $sql3 = "INSERT INTO hpics (id,p1) VALUES ('$id', '$file')"; $result3 = mysql_query($sql3) or die("Error inserting image path to database"); } } ?> What I'm trying to do is, loop through all the pictures url in the database and folder, for that 1 image that is being uploaded, and compare the names and see if they already exists. If they do exists add +1 to $nkey, and if the new name with the $nkey exists again, do another +1 to the $nkey. Until if finds a name that doesn't already exist. Is this possible? If so can someone point me in the right direction. I'm not sure if to use the for(), while(), if() or foreach() function. ??? ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/53240-looping-a-mysql_fetch_array-for-existing-var-push-in-the-right-direction-neede/ Share on other sites More sharing options...
Diego17 Posted May 28, 2007 Share Posted May 28, 2007 If they do exists add +1 to $nkey, and if the new name with the $nkey exists again, do another +1 to the $nkey. Until if finds a name that doesn't already exist.Is the important part to start with a certain $nkey and then increment that number. Or do you only need an unique number to make the filename unique? Quote Link to comment https://forums.phpfreaks.com/topic/53240-looping-a-mysql_fetch_array-for-existing-var-push-in-the-right-direction-neede/#findComment-263123 Share on other sites More sharing options...
eZe616 Posted May 28, 2007 Author Share Posted May 28, 2007 I'd prefer do start with a certain $nkey and increment it depending on the availability of that name. They way I'm thinking of doing is that. I have a Max number of 9 images that can be uploaded per ID. Let say e certain profile already has 7 pictures, and and he deletes 1, let sat # 4. If he decides to upload 3 more pictures, they will be named accordingly: pic_4,pic_8,pic_9... Where they script sees that pic_4 is open and names 1 of the images pic_4 again, then jump to _8, _9, since they are still open. Quote Link to comment https://forums.phpfreaks.com/topic/53240-looping-a-mysql_fetch_array-for-existing-var-push-in-the-right-direction-neede/#findComment-263240 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.