SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    file uploading problem (yet another)

    Ok, I know this gets brought up a lot, but I've yet to find a solution.

    We just switched to a new PHP host at work and I'm having a problem uploading files. On our old host, our upload_tmp_dir is /vservers/accountname/htdocs/uploads, so the destination path and filename (for move_uploaded_file()) is /vservers/accountname/htdocs/uploads/$filename.

    Yet in the new host the upload_tmp_dir is set to /var/tmp, which works when I use that as the destination path (but that's the only time it works). The only problem is, I don't know where the file is stored after the upload (assuming it was actually uploaded). What do I have to do to make this work?

    The code is this:
    PHP Code:
    move_uploaded_file($_FILES["mypic"]["tmp_name"], "/var/tmp/" $_FILES["mypic"]["name"]) 
    where "mypic" is the name of the input file tag. I emailed tech support but, of course, they gave me a link to php.net and told me to look there. Here's the relevant phpinfo settings:


    Virtual Directory Support disabled
    file_uploads On On
    safe_mode Off Off
    safe_mode_exec_dir no value no value
    safe_mode_gid Off Off
    safe_mode_include_dir no value no value
    upload_max_filesize 10M 10M
    upload_tmp_dir /var/tmp /var/tmp

    Thanks
    C

  2. #2
    <!-- Insert thoughts here --> pitcher17's Avatar
    Join Date
    Apr 2004
    Location
    The great white north
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the upload temp directory has write permissions turned on for the web server. Therefore, the web server is able to save the file, as a temp file, in the temp directory as soon as it is uploaded.
    You need to give the same write permissions to another directory. The second directory will be where you want to save the uploaded file.
    PHP Code:
    //$tempDir = "/var/temp/";
    $newDir "/usr/www/mysite/otherdir/uploaded_images/";
    move_uploaded_file($_FILES['mypic']['tmp_name'], $newDir $_FILES['mypic']['name']); 
    As long as $newDir has write permissions then it will work.

    You may have to ask your new host to set the permissions for you.

    HTH
    The more time I save by not planning and documenting,
    the more time I have left to debug.


  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks for the info. I email but they've yet to reply.

    Just curious - what is the php.ini setting that determines whether the destination path needs the full path or not? Our old required the /vservers/username/... but my personal host only requires a folder name (like a relative link). How do I know how to check that setting?

    C

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should never have to worry about that.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not?

  6. #6
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because $_SERVER['DOCUMENT_ROOT'] is your friend.

    PHP Code:
    $uploadDir $_SERVER['DOCUMENT_ROOT'] . '/uploads/'

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So it seems to upload fine to the temp directory. My error comes when I try to move it.

    Array ( [name] => pic.jpg [type] => image/jpeg [tmp_name] => /var/tmp/php2jgjhF [error] => 0 [size] => 22654 ) 1

    Should I just ask for $_SERVER['DOCUMENT_ROOT'] . '/uploads/' to be given write permissions?

  8. #8
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you just do that in your FTP program?

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can, but it still doesn't work.

  10. #10
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok let's see your code now.

  11. #11
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about the folders above /uploads? Do they need to be 777?

  12. #12
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It only outputs the exit() message. The $_FILES["mypic"]["error"] = 0, indicating no problem in the upload. It must be in the move_upoaded_file().


    <?php
    if($_FILES["mypic"]["size"]) {
    $dir = $_SERVER['DOCUMENT_ROOT'] . "/htdocs/uploads/";
    move_uploaded_file($_FILES["mypic"]["tmp_name"], $dir . $_FILES["mypic"]["name"]) or exit($dir);

    echo "Success: /var/tmp/" . $_FILES["mypic"]["name"];
    }
    ?>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>

    <form enctype="multipart/form-data" action="file_upload_test.php" method="post">
    <input type="file" name="mypic"><br>
    <input type="hidden" name="MAX_FILE_SIZE" value="100000">
    <input type="submit" name="submit" value="submit">
    </form>

    </body>
    </html>

  13. #13
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, don't think so.

  14. #14
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $dir = $_SERVER['DOCUMENT_ROOT'] . "/htdocs/uploads/";
    should be
    $dir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";

    You want the HTTP root, not your FTP root

  15. #15
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    htdocs is actually the folder where we keep all the PHP/mysql stuff (a folder we created). The rest of the site is in the folder above htdocs. I've tried uploading directly to htdocs, but still no go.

  16. #16
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Won't the images need to be visible to the browsing public?

    See if I have this right:

    FTP_ROOT (maybe /usr/www/mysite/)
    --WEB_ROOT (whatever IT is called...maybe httpdocs/)
    --htdocs/ (where you keep stuff)

    If that's the case, people won't be able to browse those images...correct? You also won't be able to use DOCUMENT_ROOT. And open_basedir is probably set wrong too. Let me know...

  17. #17
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DOCUMENT_ROOT is /afs/ourschool.edu/group/subgroup/ourdept/Public/html/ (where it says Host Directory in Dreamweaver)

    We keep our stuff in /afs/ourschool.edu/group/subgroup/ourdept/Public/html/htdocs
    (but I've tried it without the htdocs as well)

    upload_tmp_dir is /var/tmp

    The IT guy told me to use:
    move_uploaded_file( $_FILES['document']['tmp_name'],
    get_cfg_var( 'upload_tmp_dir' ).'/'.$_FILES['document']['name'] );

    which means the destination would be /var/tmp/$filename, which won't be accessable from the web, like you said.


    On our old host,

    DOCUMENT ROOT is /vservers/accountname/htdocs

    which is where we kept our stuff

    upload_tmp_dir is /vservers/accountname/htdocs/uploads

    which worked fine when we used
    get_cfg_var( 'upload_tmp_dir' ).'/'.$_FILES['document']['name']
    as the destination


    so I think I need to have the IT guy change our upload_tmp_dir to something that points to our document root, right?

    like /afs/ourschool.edu/group/subgroup/ourdept/Public/html/htdocs/uploads

  18. #18
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still a bit confused.
    echo $_SERVER['DOCUMENT_ROOT'] for me.

  19. #19
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /afs/myschool.edu/group/subgroup/ourdept/Public/html

    Gotta hide my identity.

  20. #20
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, so this should work:

    PHP Code:
    <?php
    if($_FILES["mypic"]["size"]) {
    $dir $_SERVER['DOCUMENT_ROOT'] . "/htdocs/uploads/";
    move_uploaded_file($_FILES["mypic"]["tmp_name"], $dir $_FILES["mypic"]["name"]) or exit($dir);

    echo 
    "Success: /var/tmp/" $_FILES["mypic"]["name"];
    }
    ?>
    SO you 777'd
    afs/ourschool.edu/group/subgroup/ourdept/Public/html/htdocs/uploads/ ?

    Do you get any error message?

  21. #21
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, it should work, but doesn't, the folder is 777. I don't get an error message except for the exit() string, which outputs the correct path.

    I'll see if the IT guy can change the upload_tmp_dir. I'm confident that's the problem after talking with a couple other people about how their hosts our set up. I have to go home anyway, but thanks for the help. I'll post my results in a day or two.

    C

  22. #22
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm...from the manual:
    If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

  23. #23
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, tech guy got back with me. He talked about some crazy stuff about ACLs and had me enter some fs sa command so that the server could write to that directory. I'm not sure I know what all that means, but everything's working now, I'm happy. Thanks guys.


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
  •