PHP - file_exists() Error

Discussion in 'PHP' started by . Jordan ., Apr 26, 2010.

  1. #1
    Okay, I'm in the final stages of making a little image hosting script and encountered a problem. What I'm attempting to do is...

    - Check if an image name already exists in the images folder.
    - Rename it using a random string if it does.
    - Then replace the extension using a function.

    Here is the the error I'm getting and the current code:

    		while($imgExists = true) {
    			if(file_exists('images/' . $imageName)) {
    				$imgExists = true;
    				$imageName = rand(1, 10) . $imageExt;
    			} else {
    				$imgExists = false;
    			}
    		}
    PHP:
    Can anyone tell me what the problem is and how I can fix it?

    Any help will be massively appreciated.
     
    . Jordan ., Apr 26, 2010 IP
  2. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Kaimi, Apr 26, 2010 IP
  3. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #3
    shouldn't this line

    while($imgExists = true) {

    use the comparison operator instead of assignment? == or = ? cause the max exec time, looks like its in an infinite loop.

    not really sure its going to work after you make that correction. probably will need to change the checked condition for the while (also not sure that's its even needed).
     
    Last edited: Apr 26, 2010
    shallowink, Apr 26, 2010 IP
  4. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    :O. Never noticed that. Thanks a tonne. I guess i just needed a fresh pair of eyes to scan through it :p

    also, thanks for the contribution, Kaimi.

    I'll edit in how it all went :)
     
    . Jordan ., Apr 26, 2010 IP
  5. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You are in an infinite loop thats why your script reaches the maximum execution time. (Your have an asignment operator in your while loop instead of an comparison operator)

    You can also simplify your code as follows:
    
    $imageName = rand(1, 10) . $imageExt;
    while(file_exists('images/' . $imageName)) {
    	$imageName = rand(1, 10) . $imageExt;
    }
    
    PHP:
     
    Nyu, Apr 26, 2010 IP
  6. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I guess so. However, I would still like to try and keep the true file names where possible. Would removing the top line achieve that do you think?
     
    . Jordan ., Apr 26, 2010 IP
  7. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Everything worked perfectly. Thanks guys xD.
     
    . Jordan ., Apr 26, 2010 IP
  8. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    What do you want to archive by removing the top line? Sry i didn't understand you question exactly^^
    Of course you can keep you code as it was without my simplification when it's easier for you to read that piece of code ;)
     
    Nyu, Apr 26, 2010 IP