Jump to content

[SOLVED] Checking If A File Exists: If It Exists, Delete It.


Vermillion

Recommended Posts

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.

Link to comment
Share on other sites

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?

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.