Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/227758-is_file-doesnt-work-as-expected/
Share on other sites

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.

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.

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).

 

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.