Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: PHP upload question

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default PHP upload question

    My girlfriend doesn't really like ftp clients, and I don't want to give her full control over the files NOT in her folder. So, I've devised a plan: I'm going to use the php ftp functions so she can just go to the page and upload, and it will go into her folder.
    I wrote out a script, but I just have a few questions.
    First off, here's the code:

    PHP Code:
    include('inc.php');

    $connect ftp_connect($address) or die("Cannot connect to server");

    $login_result ftp_login($connect$userid$passwd) or die("Cannot login to server");

    $file $_REQUEST['file'];

    ftp_put($connect$file$fileFTP_ASCII);
    ftp_close($connect); 
    Now, $address, $userid, and $passwd are in the inc.php file, and I think they're pretty self explanatory. index.php has the upload form on it, and it submits to this page. Now, first of all, how do we determine whether or not the file should be ASCII or BINARY? She'll be mostly uploading image files, so BINARY may be the way to go, but what if she uploads a .txt file or something? Then, somehow, we need to recognize this and switch it to ASCII. Is there a way to check that before uploading it?

    Also, how do we specify the folder on the server to upload to? Or is it that this file is in that directory and so it knows to just put the file in that directory?

    I haven't tested this code out yet, I wanted to get feedback first: will it even work in the first place?

    I also plan on displaying the files in the directory for her, by doing this:

    PHP Code:
    $directory_name ftp_pwd($connect);
    $filesArr ftp_nlist($connect$directory_name);
    foreach ( 
    $filesArr as $value) { echo $value."\n"; } 
    Will that work?

    And then one last thing:
    What if a file is uploaded with the same name as a file already in the directory? Is there a way to detect this and add a "2", "3", "4", etc on the end of the filename to prevent errors? Or, even throw back a message saying "Hey, there's already a file with this name?"
    Last edited by alexjewell; 10-10-2006 at 11:47 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't want to give her full control over the files NOT in her folder.
    Did you tell her about this? Ha

    Looks pretty good to me. I haven't used the ftp php functions, so not sure, though. Test it out

    As for overriding, i'm sure there's a default way to check. If by chance there isn't, just compare it to the existing files before replacing. just check the size of the file by that name, for example. If an error (use @ to not display the error), then upload the file. If not, do a confirm, or change the name of one, or, just replace the old file...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Why are you using FTP for this? Surely, since the file has already been moved to a temporary directory, it's easier to simply use move_uploaded_file() and then chmod/chown it if necessary?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Twey, the file isn't already on the server. We're uploading it from HER computer to the server, into her folder. She just needs to upload images and maybe some misc text files, etc. My two questions were basically one, how do we determine ASCII or BINARY? Is there a way to find the file extension? I may just have her select ASCII or BINARY along with the form, but I'd have to explain the difference to her.

    My other question was what if the file already exists? Djr, what exactly would that code look like? How do I check if that file already exists?

  5. #5
    Join Date
    Oct 2006
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well to check if the file exists you use this:

    if(file_exists("$filename"))

    For the extension you could use substr... example

    $extension = substr($filename,-1,3);

    That's assuming the extension is 3 chars

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Twey, the file isn't already on the server.
    But if you're handling uploads with PHP, it is. The PHP script isn't run until the file has been accepted and saved to a temporary directory.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Why do these functions exist, then?

    Give me an example of how to do this with move_uploaded_file()?

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $basedir = '/home/girlfriend/';
    
    move_uploaded_file($_FILES['file']['tmp_name'], $basedir . $_FILES['file']['name']);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by alexjewell
    Why do these functions exist, then?
    To transfer files to another server.

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Right. FTP is for remote servers. There's no point in using it on your server if you've already got access
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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