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.

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.

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.