SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast Slarti42's Avatar
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Copy / Rename / Unlink from within a function?

    I've had a file upload script working fine as part of a form script, but now I've tried to make it a function to be called on, I get errors.

    Warning: Rename failed (File exists) in c:/server/desira/news_admin.php on line 127

    Warning: Unable to open 'c:/temp/' for reading: Permission denied in c:/server/desira/news_admin.php on line 128

    Warning: Unlink failed (Permission denied) in c:/server/desira/news_admin.php on line 129

    What do I need to do to get this to work?? The script is bellow:
    PHP Code:
    function fileupload(){
        global 
    $userfile$articleId;
        
    $dir     ="c:/server/desira/upload"# your uploaded file dir, this dir require proper permission to write access 
        
    $temp     ="c:/temp";  # unix system temp dir 
        
    $maxsize ="40960"# max 40 Kb 
        
    $quota   524288# define space quota 500 Kb 
        
    $ext     = array(".p"".php"".php3"".phtml"".shtml"); # define file extension to reject 
         
        
    if ($userfile AND $userfile != "none") { 
            
    $total=0
            
    $handle=opendir($dir); 
            while (
    $file readdir($handle)) { 
                if (
    is_file("$dir/$file")) {
                    
    $total+=filesize("$dir/$file");
                }
            } 
            while (list(
    $key,$val) = each($ext)) { 
                if (
    strstr($userfile_name$val)) {
                    
    $invalidext=true;break;
                } 
            } 
            if (
    $userfile_size $maxsize) {
                echo 
    "ERROR: File too large";
            } 
            elseif (
    $invalidext) {
                echo 
    "ERROR: Forbiden file extension";
            } 
            elseif (
    $total $quota) {
                echo 
    "ERROR: Space quota exceeded";
            } 
            else { 
                
    rename("$userfile""$temp/$userfile_name"); 
                
    copy("$temp/$userfile_name""$dir/newspic_$articleId".".jpg"); 
                
    unlink("$temp/$userfile_name"); 
                
    //echo "File \"$userfile_name\" uploaded succesfully";
            
    }
        }

    This code is Copyright (C) 2001 Wibisono Sastrodiwiryo.

    Any Help greatfully recieved!!

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ifyou are gonna used any of the pre-defined vars created by an uploaded file inside of a function, you'll need to declare each one of them as global. ie. $userfile_name, or $userfile_size, etc... They each must be passed to the function or declared global inside the function. Alternately, you can simply globalize $HTTP_POST_FILES which holds all the data you need. In the form of
    PHP Code:
    $HTTP_POST_FILES['userfile']['name']
    $HTTP_POST_FILES['userfile']['size']
    $HTTP_POST_FILES['userfile']['type']
    $HTTP_POST_FILES['userfile']['tmp_name'
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Enthusiast Slarti42's Avatar
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Still not working

    Thanks for the sugestion but I still cant get this to work. I tried the two things below:

    PHP Code:
    global $HTTP_POST_FILES
    and
    PHP Code:
    global $HTTP_POST_FILES
    global $HTTP_POST_FILES['userfile']['name'];
    global $HTTP_POST_FILES['userfile']['size'];
    global $HTTP_POST_FILES['userfile']['type'];
    global $HTTP_POST_FILES['userfile']['tmp_name'];
    [/PHP]
    The second of these gave a parse error expecting "," or ";"

    Do I need to make the HTTP_POST_FILES array global or can I just use it in the function??

    Cheers

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No once you make the $HTTP_POST_FILES global, yo ucan access those array elements. You don't need to globalize each element of the $HTTP_POST_FILES array


    Its either


    PHP Code:
    function upload() {
    global 
    $userfile$userfile_name$userfile_size$userfile_type;
    print 
    $userfile_name;

    Or

    PHP Code:
    function upload() {
    global 
    $HTTP_POST_FILES;
    print 
    $HTTP_POST_FILES['userfile']['name'];

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Enthusiast Slarti42's Avatar
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    THANK YOU!!!!!!!!!!!

    Cheers for that, I used the first option you sugested and it now works a treat.

    Thanks!!


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
  •