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.
Use this function http://php.net/manual/en/function.set-time-limit.php to increase maximum execution time of your script
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).
:O. Never noticed that. Thanks a tonne. I guess i just needed a fresh pair of eyes to scan through it also, thanks for the contribution, Kaimi. I'll edit in how it all went
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:
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?
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