chaseman Posted February 15, 2011 Share Posted February 15, 2011 I have an avatar upload script. The uploading and moving of the image file to the correct directory works fine, but the displaying of the image causes problems. This is the code that is supposed to display the avatar. // _DISPLAY_ avatar - START $query2 = "SELECT * FROM user WHERE user_id = '$dbuser_id'"; $row2 = mysqli_query ($dbc, $query2) or die (mysqli_error($dbc)); $assoc2 = mysqli_fetch_assoc ($row2); $target = AVATAR_UPLOADPATH; $avatar_name = $target . $assoc2['avatar']; if (is_file($assoc2['avatar'])) { echo "<img src='$avatar_name' alt='Avatar' /><br /><br />"; } else { echo "is not a regular file"; } // END The avatar column in the MySQL database says: image.jpg So there's a regular file. What I want to accomplish: I want the script to check if there's an avatar in the database, if NOT, then it should say: "no avatar uploaded yet." But as it is now it's telling me that there's no regular file even if there is, as I said the avatar column has an entry saying: image.jpg. Should I use if !empty instead? The strange thing is it used to work, now for some reason it suddenly doesn't. EDIT: With !empty it works, am I using the is_file function incorrectly? Quote Link to comment https://forums.phpfreaks.com/topic/227758-is_file-doesnt-work-as-expected/ Share on other sites More sharing options...
BlueSkyIS Posted February 15, 2011 Share Posted February 15, 2011 make sure you are passing a valid path to is_file. otherwise the file really does not exist. Quote Link to comment https://forums.phpfreaks.com/topic/227758-is_file-doesnt-work-as-expected/#findComment-1174570 Share on other sites More sharing options...
chaseman Posted February 15, 2011 Author Share Posted February 15, 2011 First thing is, as I said it works with !empty... so the file is definitely there. Second thing is, how does the path of is_file has to look like? Is the filename itself enough or does it need a directory too? $assoc2['avatar'] refers JUST to the column, and INSIDE the column the FILENAME is inserted as image.jpg.... so basically $assoc2['avatar'] stands for "image.jpg".... do I have to tell the is_file function in which directory it's located as well? I have the image files in the folder avatar, which is in the root directory. Quote Link to comment https://forums.phpfreaks.com/topic/227758-is_file-doesnt-work-as-expected/#findComment-1174627 Share on other sites More sharing options...
Jessica Posted February 15, 2011 Share Posted February 15, 2011 do I have to tell the is_file function in which directory it's located as well? Yes. otherwise how would it work if you have files with the same name, in different directories? Quote Link to comment https://forums.phpfreaks.com/topic/227758-is_file-doesnt-work-as-expected/#findComment-1174628 Share on other sites More sharing options...
jcbones Posted February 16, 2011 Share Posted February 16, 2011 is_file() doesn't just check to make sure it is a valid filename, it checks that the name given is an actual file. So therefore, checking the name in a database column will always return false, as a name in a database is NOT a file. Quote Link to comment https://forums.phpfreaks.com/topic/227758-is_file-doesnt-work-as-expected/#findComment-1174763 Share on other sites More sharing options...
chaseman Posted February 16, 2011 Author Share Posted February 16, 2011 is_file() doesn't just check to make sure it is a valid filename, it checks that the name given is an actual file. So therefore, checking the name in a database column will always return false, as a name in a database is NOT a file. Yea I've looked in the manual, and honestly I found the explanation too minimalistic. The way I've understood it from the example it checks for the ending, .jpg, .txt, .gif, etc. But I now understand that a REFERENCE in the database is not enough, instead the is_file looks for the actual file on the server. So in my initial version it looked for the file in the root directory, since there was no file it returned a false. So to make it work I'd have to do a: $avatar_name = $assoc2['avatar']; $avatar_directory = AVATAR_UPLOADPATH; // which is 'avatar/' in the root directory // and then: if (is_file($avatar_directory . $avatar_name)) { - run script - } else { echo "error"; } Haven't tested this, but I think this should work, now it's going directly to the directory and checks if the file is there and if it's a regular file. Since assoc grabs the filename off the database I was thinking in terms of database, which is a wrong approach. I should perceive the database as an index like in a book and the actual content is on the server in the directories (chapters). Quote Link to comment https://forums.phpfreaks.com/topic/227758-is_file-doesnt-work-as-expected/#findComment-1174785 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.