Jump to content

file_exists issue?


Allenph9

Recommended Posts

 

 

<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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.