www.webdeveloper.com
Results 1 to 6 of 6

Thread: Check before upload File

  1. #1
    Join Date
    Sep 2004
    Posts
    71

    Check before upload File

    Hello, Im creating a system of uploading files. I want to know if there is a way I can check if the file that is being uploaded already exists at that destination before I actually upload the file, and if it is, alert the user about that before doing anything.

    To upload the files Im using;

    move_uploaded_file(string $filename, string $destination)

    thanks a lot

  2. #2
    Join Date
    Dec 2004
    Posts
    8,637
    The technical answer is, No. By the time your PHP code starts executing, the file has already been uploaded and stored under the temporary file name and location. At that point, you can certainly check if the permanent file name already exists at the location to which you plan to move the file. Is that what you want?

    Check out the file_exists() function.

    A related topic -- and possible alternate method of handling existing file names:

    http://www.webdeveloper.com/forum/sh...threadid=56959
    Last edited by phpnovice; 02-28-2005 at 03:29 PM.

  3. #3
    Join Date
    Nov 2003
    Location
    England
    Posts
    80
    To check that the temporary file has been uploaded ok, use the is_uploaded_file function;


    if (is_uploaded_file($temp))
    {
    move_uploaded_file(string $filename, string $destination);
    }
    else
    {
    //nothing there
    }

    Then use file_exists to check that the file is on your server.

    dc
    Voyager Web Design UK
    Web Design::Custom Programming::Custom Graphics
    -------------------------------------------------------
    M-Dream
    Asian Cinema::Asian Music::Asian Cover Scans::Chinese Astrology::Free PHP Scripts::Hong Kong Photo Galleries

  4. #4
    Join Date
    Sep 2004
    Posts
    71
    Thanks guys I got it to work, here is how I did it;

    ---------------------------------------------
    $uploadDir = 'docs/' . $dirpath_artefato . '/';
    $uploadFile = $uploadDir . $_FILES['userfile']['name'];
    $num = '';
    if (file_exists ($uploadFile)) {
    $num = '1';
    while (file_exists ($uploadDir . '/' . $num . '_' . $_FILES['userfile']['name'])) {
    $num++;
    }
    $uploadFile = $uploadDir . '/' . $num . '_' .$_FILES['userfile']['name'];
    }

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile))
    {
    // code ****inues

  5. #5
    Join Date
    Dec 2004
    Posts
    8,637
    Awww, you put the sequence number on the beginning of the file name. Why did you not want to put the sequence number on the end of the file name (before the file extension) as I supplied the code in that other thread link I gave you?

  6. #6
    Join Date
    Sep 2004
    Posts
    71
    just personal preference,

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles