Allenph9 Posted March 5, 2012 Share Posted March 5, 2012 <code> $jpg_path = '../../../users/user_avatars/' . $_SESSION['the_user'] . '.jpg'; $jpeg_path = '../../../users/user_avatars/' . $_SESSION['the_user'] . '.jpeg'; $gif_path = '../../../users/user_avatars/' . $_SESSION['the_user'] . '.gif'; $png_path = '../../../users/user_avatars/' . $_SESSION['the_user'] . '.png'; $jpg_test = file_exists($jpg_path); $jpeg_test = file_exists($jpeg_path); $gif_test = file_exists($gif_path); $png_test = file_exists($png_path); </code> the path is EXACTLY correct I've checked and rechecked and everything. echoed the path and everything else that is relevant. but later when I call if ($png_test == '1') { blah blah blah } it won't return 1. even though I KNOW that the picture is there and it is at that path. if i echo the path in a src for an image it appears. hmm? Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 5, 2012 Share Posted March 5, 2012 try it with the following function, I've had similar problems with file_exists, and I found this function that worked for me: <?php function fileExists($path){ return (@fopen($path,"r")==true); } ?> Quote Link to comment Share on other sites More sharing options...
Allenph9 Posted March 5, 2012 Author Share Posted March 5, 2012 I honestly don't understand what that does. i have completely ignored function and return and @ up till now. I never really needed them. What does this do? it seems like it would just return true no matter what path you put in there. how do I call the function? how do i use it in a variable or if statement? sorry for being so ignorant. thanks! Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 5, 2012 Share Posted March 5, 2012 I honestly don't understand what that does. i have completely ignored function and return and @ up till now. I never really needed them. What does this do? it seems like it would just return true no matter what path you put in there. how do I call the function? how do i use it in a variable or if statement? sorry for being so ignorant. thanks! put the function somewhere at the top of the php page, and instead of typing file_exists(), which is a php library function, call fileExists, which is the custom function, so your code would look like <?php function fileExists($path){ return (@fopen($path,"r")==true); } //stuff here $jpg_test = fileExists($jpg_path); $jpeg_test = fileExists($jpeg_path); $gif_test = fileExists($gif_path); $png_test = fileExists($png_path); //other stuff ?> Quote Link to comment Share on other sites More sharing options...
Allenph9 Posted March 5, 2012 Author Share Posted March 5, 2012 Now when I echo the test it is nothing. Is it setting it to true or 1? Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 5, 2012 Share Posted March 5, 2012 Look at the function, if it determines the file to exist, it returns true. Yeah you shouldn't do your statements like ($whatever == '1'){ } do them like if($whatever) { } Quote Link to comment Share on other sites More sharing options...
Allenph9 Posted March 5, 2012 Author Share Posted March 5, 2012 hmm...that does nothing either my friend...am i doing something wrong? my code is <code> $jpg_path = '../../../users/user_avatars/' . $_SESSION['the_user'] . '.jpg'; $jpeg_path = '../../../users/user_avatars/' . $_SESSION['the_user'] . '.jpeg'; $gif_path = '../../../users/user_avatars/' . $_SESSION['the_user'] . '.gif'; $png_path = '../../../users/user_avatars/' . $_SESSION['the_user'] . '.png'; function fileExists($path){ return (@fopen($path,"r")==true); } $jpg_test = fileExists($jpg_path); $jpeg_test = fileExists($jpeg_path); $gif_test = fileExists($gif_path); $png_test = fileExists($png_path); $body1 = htmlentities($_POST['thread_body']); $body = htmlspecialchars($body1 ,ENT_QUOTES); $new_thread_file_link = '<?php $the_thread_title = ' . '\'' . $_POST['thread_title'] . '\'' . '; ?>' . $template_contents; $new_thread_file_link1 = '<?php $thread_post_body = ' . '\'' . $body . '\'' . '; ?>' . $new_thread_file_link; if ($jpg_test) { $new_thread_file_link11 = '<?php $the_avatar_path = ' . '\'' . '../../../users/user_avatars/' . $_SESSION['the_user'] . '.jpg' . '\'' . '; ?>' . $new_thread_file_link1; } elseif ($jpeg_test) { $new_thread_file_link11 = '<?php $the_avatar_path = ' . '\'' . '../../../users/user_avatars/' . $_SESSION['the_user'] . '.jpeg' . '\'' . '; ?>' . $new_thread_file_link1; } elseif ($gif_test) { $new_thread_file_link11 = '<?php $the_avatar_path = ' . '\'' . '../../../users/user_avatars/' . $_SESSION['the_user'] . '.gif' . '\'' . '; ?>' . $new_thread_file_link1; } elseif ($png_test) { $new_thread_file_link11 = '<?php $the_avatar_path = ' . '\'' . '../../../users/user_avatars/' . $_SESSION['the_user'] . '.png' . '\'' . '; ?>' . $new_thread_file_link1; } else { $new_thread_file_link11 = '<?php $the_avatar_path = ' . '\'' . '../../../users/user_avatars/' . 'default_avatar.png' . '\'' . '; ?>' . $new_thread_file_link1; } </code> Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 5, 2012 Share Posted March 5, 2012 Since you're not getting the response you're looking for, you need to simplify your code until you get something correctly. Do this as your entire page: <?php $png_path = '../../../users/user_avatars/' . $_SESSION['the_user'] . '.png'; function fileExists($png_path){ return (@fopen($path,"r")==true); } if(fileExists($png_path)) echo "It exists"; else echo "It doesn't"; ?> Also, you're using session variables, but are you doing session_start() at the top of this page? Quote Link to comment Share on other sites More sharing options...
Allenph9 Posted March 5, 2012 Author Share Posted March 5, 2012 the result is it dosnt exist even though it does exist!!!!! Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 5, 2012 Share Posted March 5, 2012 I screwed up the code I gave, fileExists($png_path) should be fileExists($path) And I tested the code in mine and it works, so the part where you use $_SESSION variable has to be wrong. Replace that with the actual value of something that you know exists. I'm sure your session is not starting or something Quote Link to comment Share on other sites More sharing options...
l0gic Posted March 5, 2012 Share Posted March 5, 2012 Are you on a Windows or Linux/Unix based server? Windows: $png_path = '..\..\..\users\user_avatars\' Linux/Unix: $png_path = '../../../users/user_avatars/' Quote Link to comment Share on other sites More sharing options...
Allenph9 Posted March 5, 2012 Author Share Posted March 5, 2012 thank you very much...this code works with whatever extension it has been tested... $jpg_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.jpg'; $jpeg_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.jpeg'; $gif_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.gif'; $png_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.png'; $png_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.png'; function fileExists($png_path){ return (@fopen($png_path,"r")==true); } if(fileExists($png_path)) $png_test = '1'; function fileExists1($gif_path){ return (@fopen($gif_path,"r")==true); } if(fileExists1($gif_path)) $gif_test = '1'; function fileExists2($jpg_path){ return (@fopen($jpg_path,"r")==true); } if(fileExists2($jpg_path)) $jpg_test = '1'; function fileExists3($jpeg_path){ return (@fopen($jpeg_path,"r")==true); } if(fileExists3($jpeg_path)) $jpeg_test = '1'; Quote Link to comment Share on other sites More sharing options...
Allenph9 Posted March 5, 2012 Author Share Posted March 5, 2012 Are you on a Windows or Linux/Unix based server? Windows: $png_path = '..\..\..\users\user_avatars\' Linux/Unix: $png_path = '../../../users/user_avatars/' Windows but im using what you are saying is linux and it is working fine Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 5, 2012 Share Posted March 5, 2012 thank you very much...this code works with whatever extension it has been tested... $jpg_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.jpg'; $jpeg_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.jpeg'; $gif_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.gif'; $png_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.png'; $png_path = '../../users/user_avatars/' . $_SESSION['the_user'] . '.png'; function fileExists($png_path){ return (@fopen($png_path,"r")==true); } if(fileExists($png_path)) $png_test = '1'; function fileExists1($gif_path){ return (@fopen($gif_path,"r")==true); } if(fileExists1($gif_path)) $gif_test = '1'; function fileExists2($jpg_path){ return (@fopen($jpg_path,"r")==true); } if(fileExists2($jpg_path)) $jpg_test = '1'; function fileExists3($jpeg_path){ return (@fopen($jpeg_path,"r")==true); } if(fileExists3($jpeg_path)) $jpeg_test = '1'; You're not using functions properly. You can use one function for all those tests, fileExists is fine with $path in the parenthesis and $path in the fopen() function, if you're gonna put the same code 4 times, there's no reason to even use functions. Their purpose is to make things easier Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2012 Share Posted March 5, 2012 You are making this far far too hard with too much hard-coded logic. If you are just trying to find if there is an avatar file or not and to get the file name if there is, all you would need to do is use the following - <?php $filename = '../../../users/user_avatars/' . $_SESSION['the_user']; $files = glob($filename.".*"); // get an array of all matching path/filename.* files if(empty($files)){ echo 'No avatar file found for the user.'; } else { // the $files array contains the path and filename.ext for the matching file(s) } If your code is not working, you likely have a variable scope problem or your logic is clearing the values and you would need to post your exact code that reproduces the problem to get help with it. FYI - under Windows, php converts / path separator characters into \ before making the low-level calls to the operating system functions. You can use / as path separator characters in all php code regardless of the operating system it is running under. Quote Link to comment 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.