SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Uploaded files disappear

    I created a file upload script for a new website. The script is basically a slightly modified version of an upload script I use on another site. The script works perfect on the original site, but not this new site.

    The script runs without displaying any errors. It has no problem collecting data about an uploaded file (such as filesize, dimensions, etc). However, when the script is complete, the actual file is nowhere to be found.

    This is basically how my script handles files:

    PHP Code:
    $filename $_FILES["submission"]["tmp_name"];

    $source = ("_submissions/".$record_id."_".preg_replace("/[^a-z\d]/i","_",substr($title050)).".".$file_extension);

    move_uploaded_file($filename$source); 
    The uploaded file cannot be found in the '_submissions' folder or anywhere else on the server. Any ideas?

  2. #2
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check the permissions on the folder.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Permissions are set correctly.

    Edit: I also tried using the copy() function, rather than move_uploaded_file(), but it failed to work as well.

  4. #4
    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)
    *GUESS*

    What setting is in the php.ini file for:

    upload_tmp_dir

    Perhaps that has some bearing on the problem.

  5. #5
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    phpinfo() shows 'no value' for 'upload_tmp_dir' under both, the Local and Master values.

    I do not think the host of this site allows me to freely modify the php.ini file. The file that displays phpinfo() is located in the main 'public_html' directory, however, phpinfo() shows the 'Loaded Configuration File' as '/usr/local/lib/php.ini'.

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try change the source folder to another path (simple path like : upload/files/$_FILES["submission"]["name"]; )

    and then try again using move_uploaded_file function and check if it uploaded or no ,

  7. #7
    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)
    Seems fundamental then that you should then query these values.

    Quote Originally Posted by php.ini file
    ;;;;;;;;;;;;;;;;
    ; File Uploads ;
    ;;;;;;;;;;;;;;;;

    ; Whether to allow HTTP file uploads.
    file_uploads = On

    ; Temporary directory for HTTP uploaded files (will use system default if not
    ; specified).
    upload_tmp_dir = c:\temp

    ; Maximum allowed size for uploaded files.
    upload_max_filesize = 2M
    Do that using ini_get()

  8. #8
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    I tried an alternative path already.

    I have also noticed I cannot create files. Part of my upload script allows users to create a text file:

    PHP Code:
    $filename = ('_temp_files/'.random_string('alnum'12));
    $new_file fopen($filename"w");
    fwrite($new_file$_POST['text'], 5000000);
    fclose($new_file); 
    No error message is displayed, but no file is created. Again, this scripting works fine on the site I originally created it for.


    By the way, the host claims custom php.ini is allowed, but I cannot seem to access the primary pre-defined file.

  9. #9
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    Quote Originally Posted by Cups View Post
    Seems fundamental then that you should then query these values...
    file_uploads = 1

    upload_tmp_dir =
    (no value)

    upload_max_filesize = 128M


    It seems the problem is greater than just uploads though. (see my last post)

    Btw, perms on the upload folder are 0755, the same as the upload folder on my original site.

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    print_r(stat('path to the problem dir'));
    var_dump(getmyuid());
    var_dump(getmygid()); 
    What do you get?

    The errors are in your error_log btw, your host probably just has display_errors disabled(which is good).

  11. #11
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    Well there is no single problem directory. I cannot seem to upload to any directory, or create files in any directory. I went ahead and created a script with the following code:

    PHP Code:
    <?php

    print_r
    (stat('/_submissions'));

    var_dump(getmyuid());

    var_dump(getmygid()); 

    ?>
    Here is what was returned:

    Code:
    Array ( [0] => 2056 [1] => 121687554 [2] => 16877 [3] => 3 [4] => 32766 [5] => 32768 [6] => 0 [7] => 4096 [8] => 1229227436 [9] => 1229227436 [10] => 1229227436 [11] => 4096 [12] => 16 [dev] => 2056 [ino] => 121687554 [mode] => 16877 [nlink] => 3 [uid] => 32766 [gid] => 32768 [rdev] => 0 [size] => 4096 [atime] => 1229227436 [mtime] => 1229227436 [ctime] => 1229227436 [blksize] => 4096 [blocks] => 16 ) int(32766) int(32768)
    The UID and GID are not exactly the same. Could this be the problem?

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm. Those permissions should work. I was looking to see if the user php runs as is the same as the owner of the dir, which it is.

    What errors do you get?
    check php's error log, or turn on display_errors via ini_set()

    Maybe you have an open_basedir restriction.

  13. #13
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    Oh, I forgot to mention that. The error log in the cPanel shows no errors at all for this script.

    Although, you might still be on to something. In phpinfo(), 'open_basedir' is the only core setting that has a different value under Local than the value under Master.

    The Local Value for 'open_basedir' is:
    /home/****/:/usr/lib/php:/usr/local/lib/php:/tmp
    (where the asteriks represent the username)

    The Master Value is not set. (no value)

  14. #14
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well there you go.
    You can't go outside of these filepaths.

    /home/****/
    /usr/lib/php
    /usr/local/lib/php
    /tmp

    And you're trying to use
    /_submissions

    Anyway, php should be throwing errors for this. You just need to set error_reporting to an appropriate level. E_ALL would surely report it all.
    But you need to add a new path to the open_basedir value if you want this to work.

  15. #15
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Contrary to what this host claims, it doesn't seem that I can override the values of the inaccessible Master php.ini file by adding a php.ini file to the site's root directory.

    In the site's root directory, I now have a php.ini file that looks exactly like this:

    Code:
    memory_limit = 256MB
    post_max_size = 110MB
    upload_max_filesize = 100MB
    upload_tmp_dir = /_temp_files
    open_basedir = /
    I also have a phpinfo file in the site's root directory. The phpinfo file still shows the 'Loaded Configuration File' as '/usr/local/lib/php.ini'.

    Settings, such as 'upload_max_filesize', do not match what is defined in the php.ini file located in the same directory.

    'open_basedir' also has not changed.


    Here is what the host's support center claims about modifying 'upload_max_filesize':

    How do I change my upload_max_filesize?
    Solution
    Locate the php.ini file in the folder your upload script resides in. If none exists, create a new text file called php.ini
    In the php.ini file, locate the line called 'upload_max_filesize = 2M'
    Modify that entry to read 'upload_max_filesize = 10M', or whatever your script requires. (M = megabytes)
    That's it!
    Obviously, this is not the case for upload_max_filesize, or any other configuration setting.

    Do you think I need to contact the host?

  16. #16
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sometimes, the php.ini files needs to be in the same dir as the script being run via url. Kinda a pain, because this can mean you need a copy in many dirs.

    Sometimes, you must place a setting in a .htaccess file to tell it the location of the php.ini file.

  17. #17
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A copy of that php.ini file exists in all sub-directories.


    Quote Originally Posted by crmalibu View Post
    Sometimes, you must place a setting in a .htaccess file to tell it the location of the php.ini file.
    Do you know how exactly to do this? Checking the web didn't seem to help.

  18. #18
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, first of all: your original post code used the relative _submissions directory that resides in the same location as your upload script. This is not the /_submissions that you used in crmalibu's code snippet.

    Your test fopen() code also used a relative directory. You used _temp_files/ and not /_temp_files/.

    However, when you did use /_submissions with crmalibu's code snippet, it returned files and the directory did exist. Where did these files come from?

  19. #19
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How exactly should slashes be used for relative directories?

    On the site in which my upload script was originally created for, all the sources for uploads are something like '_submissions/123.jpg'.

    I never used a slash in front, and it worked fine. I understand that this site may be a different case... but in general, when should slashes be used in front of or after a directory name?

    e.g:
    /_submissions
    _submissions/
    /_submissions/

  20. #20
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say your script resides in /home/bobbytables/uploader/. When your script runs, it will be aware of its "current directory." In this case, the script's current directory is "/home/bobbytables/uploader/". When you do file_get_contents("test.txt"), you are referring to a file using a relative path. test.txt, as relative to the current directory, is /home/bobbytables/uploader/test.txt. However, if you do file_get_contents("/test.txt"), then you are using an absolute path. So instead of accessing /home/bobbytables/uploader/test.txt, you will be accessing /test.txt, which resides at the very root of the file system. If you want to refer to the test.txt in the current directory using an absolute path, you would do file_get_contents("/home/bobbytables/uploader/test.txt").

    Summary:
    Your current directory: /home/bobbytables/uploader/
    (Where your script resides in this example.)

    If you want the file at /home/bobbytables/uploader/test.txt:
    Absolute path: file_get_contents("/home/bobbytables/uploader/test.txt")
    Relative path (from the current directory): file_get_contents("test.txt")

    If you want the file at /test.txt:
    Absolute path: file_get_contents("/test.txt")
    Relative path (from the current directory): file_get_contents("../../../test.txt")

    (../ moves "up" one directory, if you didn't know.)

    If you've ever used cd in MS-DOS or command prompt, you changed the current (working) directory.

    So, if you want to refer to the _submissions folder at the root of the file system, then you do /_submissions (using a relative path here with a bunch of ../ is a bit crazy). However, if you want the _submissions folder in the current folder of the script, you do just _submissions. Or, if you want the _submissions folder at /home/****/_submissions, then you do /home/****/_submissions (or you can do a relative path like ../_submissions, depending on where the script is).

    The / at the end of the path of a directory does not matter.

  21. #21
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My destination sources are indeed in the format of '_submissions/filename.ext'. The code snippet I posted earlier was simply a typo. The actual script did not have a slash in front.

    I contacted the host and they said they fixed the problem. I can now modify many of the PHP directives, but some settings, such as 'open_basedir' and 'display_errors', do not appear to be changed in the phpinfo.

    If I want my scripts to have access to any place within the site's main root directory, what should I try setting 'open_basedir' to?

  22. #22
    SitePoint Enthusiast VideoWhisper's Avatar
    Join Date
    Dec 2008
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Contact your host and tell them to allow write permissions for your php scripts as default.
    Otherwise you will need to change file/folder owner and permissions before writing.

  23. #23
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This host just does not seem to want to change the open_basedir directive. And now that I have finally been able to active display_errors, I am certain this is the problem. My upload script now gives me this error:

    Code:
    Warning: imagejpeg() [function.imagejpeg]: Unable to open '_submissions/thumbnails/18.jpg' for writing: Permission denied in /home/accountname/public_html/domainname/_scripts/submit_content.php on line 259
    
    Warning: move_uploaded_file(_submissions/18_test.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/accountname/public_html/domainname/_scripts/submit_content.php on line 286
    
    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpotVrX9' to '_submissions/18_test.jpg' in /home/accountname/public_html/domainname/_scripts/submit_content.php on line 286

  24. #24
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Recent versions of php report open_basedir restrictions in the error message. Plus, given the values you provided for your open_basedir setting, you would not have a problem anyway.

    You are most likely trying to write to this directory:
    /home/accountname/public_html/domainname/_scripts/_submissions/

    Make it writable by php.

    You can find out for sure which dir you're trying to write to by inspecting php's current working directory at the point in your script where you try to do these file operations. see getcwd(). relative file paths will be relative to this dir. absolute filepaths are relative to nothing.

  25. #25
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this post was very usefull to me..thx u 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
  •