SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question move_uploaded_file doesn't move files - problem with apache configuration in OpenSuse

    Hi,

    I’m new to PHP and am trying to figure out how to make move_uploaded_file to work.
    To test and learn PHP I use public_html folder in my home dir, that is: /home/wojtek/public_html. I access it like this: localhost/~wojtek/[…]
    The server root is /srv/www/htdocs.

    Let me show the code. It comes from ‘Head First PHP’ book. Here you go: http://pastie.org/4809762

    The problem is that an uploaded file doesn't reach the temporary folder for uploaded files. I set it to ‘/srv/www/tmp’. Here is the output of ‘ls’ command:
    Code:
    drwxrwxrwx 2 root www  4096 Sep 26 17:33 tmp
    I don’t even know where the temporary folder should go

    Apache belongs to ‘www’ group so I added myself to that group too.

    PHP Code:
    require_once('appvars.php'); 
    contains:
    PHP Code:
    <?php
        
    // define application constants
        
    define('GW_UPLOADPATH''/home/wojtek/public_html/headfirst/ch05/guitarwars/images/');
    ?>
    And here is another problem.* I don’t altogether know what path should I use – absolute or relative? I tried with:
    http://localhost/~wojtek/[…]
    • /~wojtek/[…]
    • images/ (the script is in guitarwars/)

    and none worked.

    The script works. I get no errors. And I even get the temporary name of an uploaded file.

    I tried somehow make $_SERVER['DOCUMENT_ROOT'] to work – to get the root of public_html but I always get /srv/www/htdocs.

    So I’m completely confused. I don’t know:
    • where should temporary folder be created (in /srv/www/[…] or in public_html?)
    • why is the temporary folder always empty (no files even when there is no error when file is uploaded)
    • what destination path of move_uploaded_file should I*use (absolute, relative (relative to what?))

    I hope this is not a tricky question and you guys can help me. Thanks in advance!

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You are asking a lot of questions, so people are less likely to take the trouble to reply.

    I think the crux of your problem is to do with locating tmp directory for PHP (a google results page), although you may have compounded problems with permissions by the sound of it.

    The other thing I always fully recommend, not matter how brilliant your book is, is to study fully the Handling File Uploads section of the online manual.

    The trick is to identify, isolate and fix one challenge at a time.

    Now, did you find your /tmp directory?

    Can you upload anything to that directory?

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    You are asking a lot of questions, so people are less likely to take the trouble to reply.

    I think the crux of your problem is to do with locating tmp directory for PHP (a google results page), although you may have compounded problems with permissions by the sound of it.

    The other thing I always fully recommend, not matter how brilliant your book is, is to study fully the Handling File Uploads section of the online manual.

    The trick is to identify, isolate and fix one challenge at a time.

    Now, did you find your /tmp directory?

    Can you upload anything to that directory?
    Agreed, and check your phpinfo()! It will identify a lot of what you are asking, the tmp directory should be defined, the SERVER['DOCUMENT_ROOT'] is defined (hint: this is set in your Apache configuration).

    Also verify the folder you want to move the file to is writeable or has proper permissions for your apache user (usually wwwdata) to access the folder.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, sorry for writing about too many things. First things first.*

    The temp dir is located at:
    Code:
    upload_tmp_dir = "/srv/www/tmp"
    output of ls -hal:
    Code:
    drwxrwxrwx 2 root www  4.0K Sep 27 13:00 tmp
    I can copy files there manually without an error.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Which user are you and which user is Apache?

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    wojtek@linux-4h22:~$ groups
    users www video
    Code:
    wojtek@linux-4h22:/etc/apache2$ cat uid.conf
    User wwwrun
    Group www

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I*added at the top of the script these lines:
    PHP Code:
    error_reporting(E_ALL);
    ini_set("display_errors"1); 
    and now get this:
    Code:
    Warning: move_uploaded_file(/home/wojtek/public_html/headfirst/ch05/guitarwars/images/001.jpg): failed to open stream: Permission denied in /home/wojtek/public_html/headfirst/ch05/guitarwars/addscore.php on line 38 Warning: move_uploaded_file(): Unable to move '/srv/www/tmp/phpCb8m0M' to '/home/wojtek/public_html/headfirst/ch05/guitarwars/images/001.jpg' in /home/wojtek/public_html/headfirst/ch05/guitarwars/addscore.php on line 38 upload failed
    Which is much clear now. So the problem must be with the destination.

    Following the advice on php.net, I*changed the user of images/ to ‘nobody’ and its rights to ’755’. But still with no success.

    // edit
    OK, I restored the ownership to my user (wojtek) and gave it 777 rights. Now it works!

    The next issue that bothers me is how should I*set the destination? Is it OK*to give the whole, absolute path like this one:
    PHP Code:
        // define application constants
        
    define('GW_UPLOADPATH''/home/wojtek/public_html/headfirst/ch05/guitarwars/images/'); 
    ?

    Can I*for example to make use of relative paths? Kinda ‘/~wojtek/headfirst/ch05/guitarwars/images/’?

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. Of course I*can use relative paths now.
    Thank everybody for interest!


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
  •