Vermillion Posted August 31, 2008 Share Posted August 31, 2008 On my forum, people will have the ability to upload avatars (obviously =P). The name of the file will be set to the ID of the user in the Database. The script looks like this. The $id variable is declared before this block of code: <fieldset style="width:720px;"> <legend>Upload Avatar</legend> <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="file" name="avatar" /><input type="submit" value="Submit" /> </form><?php if($_FILES['avatar']['name']){ $maxfilesize = 25600; $_FILES['avatar']['name'] = $id; $avvy = $_FILES['avatar']['name']; $filename = $avvy; $path = "../../graphics/avatars/$filename"; echo $path; } else { echo " The file must not be larger than 75 x 75 pixels and no bigger than 25 KBs."; } ?> </fieldset> How can I check if a file exists? Also, I will allow both JPG/JPEG/ and GIF avatars. How can I go around to retrieve the extension of the file name? I'm sure I will need to know the whole file name if I want to check if it exists and then delete it. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 31, 2008 Share Posted August 31, 2008 Check out file_exists() and strrchr() for the extension. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 31, 2008 Share Posted August 31, 2008 Wouldn't it be an awful lot easier to store the name of the file in your database? Then you wouldn't have the problem of the unknown file extension. You could, however, use the glob() function which allows you to find files whilst using wildcards. Quote Link to comment Share on other sites More sharing options...
Vermillion Posted August 31, 2008 Author Share Posted August 31, 2008 Wouldn't it be an awful lot easier to store the name of the file in your database? Then you wouldn't have the problem of the unknown file extension. You could, however, use the glob() function which allows you to find files whilst using wildcards. Yeah people have recomended me that, but I am not quite sure it would work. Do I only store the name of the file without the extension? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 31, 2008 Share Posted August 31, 2008 No, you store the whole file name so it can be easily manipulated via MySQL rather than the filesystem. 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.