Results 1 to 5 of 5

Thread: Check to see if file exists?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Check to see if file exists?

    I'm using a simple method to display 'avatars' on my page. Rather than uploading a users picture, I simply use the URL they provide. To make sure the link is a picture I check from the right and up to the first '.' encountered. If I get 'jpg/jpeg/bmp/png/gif' returned then the link is a picture.

    However what I would also like to do is, if the link no longer exists or cannot be found, show a generic picture.

    Any ideas on how to do this bit?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check to see if file exists?

    What you could do is download the file from the URL they provide and load it using the GD library (if you have that installed). Using this method, you can both check that it exists and that it is a valid image without having to resort to parsing the URL (which could fail in valid circumstances; for example, if the remote host uses URL rewriting).
    You can then display the local downloaded copy of the image, which avoids bandwidth theft.

    A similar method is employed on this forum. If you edit your avatar you will notice an option to upload a file from a remote URL.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Check to see if file exists?

    The reason I didn't want to download files from URLs provided is memory issues. Should I be concerned by this?

  4. #4
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Check to see if file exists?

    try file_exists($file);
    My usual boring signature: Something

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Check to see if file exists?

    Quote Originally Posted by wwwfilmfilercom
    The reason I didn't want to download files from URLs provided is memory issues. Should I be concerned by this?
    Memory, or storage space?

    If it's storage space, then impose a file size limit.


    Quote Originally Posted by dclamp
    try file_exists($file);
    That only works for local files and some URL wrappers, of which HTTP isn't one of them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width