Topic: Adding a upload script into profiles

Hi,

Im trying to a upload form/script to the profiles menu, ive done everything, made the form, but the actual upload script is troubling me.
I was thinking of copying the upload methods that punbb use to upload avatars but i cant find the actual script, all i can see is when it checks for errors and stuff.
I tryed using normal copy methods for the script:

copy($file_name,PUN_ROOT.'/uploads/'.$file_name);

but that just returns an error that it cannot find the file specified. This is when I already browsed the file and clicked submit to proccess the form.

Any ideas on how I should go about on doing this.

I heard the use of headers, do I need to use them?

Thanks in advance

Re: Adding a upload script into profiles

if(is_uploaded_file($_FILES['file']['tmp_name']) && !file_exists($_FILES['file']['name']))
{
move_uploaded_file($_FILES['file']['tmp_name'], PUN_ROOT."uploads/".$_FILES['file']['name']);
redirect($_SERVER['PHP_SELF'], "File uploaded");
}

This should do...

Re: Adding a upload script into profiles

tryed that and it only worked once, but the file it uploaded was 0kb, when its an image file on my comp and its like 200kb.
The other times i tryed it didnt work at all

Re: Adding a upload script into profiles

Minor error there... but else it should work:

<?php
if(is_uploaded_file($_FILES['file']['tmp_name']) && !file_exists(PUN_ROOT."uploads/".$_FILES['file']['name']))
{
    move_uploaded_file($_FILES['file']['tmp_name'], PUN_ROOT."uploads/".$_FILES['file']['name']);
    redirect($_SERVER['PHP_SELF'], "File uploaded");
}
?>

Make sure ['file'] resembles the name of the form element.

Re: Adding a upload script into profiles

Still doesnt work, its like it doesnt wait for the upload to complete, it redirects straight away. This is what I got so far:

$file_name = $_POST['file'];
    $file_desc = $_POST['desc'];
    
    if(is_uploaded_file($_FILES['file_name']['tmp_name']) && !file_exists(PUN_ROOT."uploads/".$_FILES['file_name']['name']))
    {
        move_uploaded_file($_FILES['file_name']['tmp_name'], PUN_ROOT."ebooks/".$_FILES['file_name']['name']);
        redirect($_SERVER['PHP_SELF'], "File uploaded");
    }

Re: Adding a upload script into profiles

Can you post your HTML too?

7 (edited by Canibal 2006-07-06 14:53)

Re: Adding a upload script into profiles

Here is the html code that shows the extra link in the profiles tab and what it shows if its selected:

else if ($section == 'ebooks')
    {
    
        $page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_common['Profile'];
        require PUN_ROOT.'header.php';
        
        //Generates the profile menu
        generate_profile_menu('ebooks');
        
        //Creates the upload form
        ?>
        
        <div class="blockform">
        <h2><span><?php echo pun_htmlspecialchars($user['username']).' - '.$lang_profile['Section Ebooks'] ?></span></h2>
        <div class="box">
        
        <form id="profile8" method="post" action="profile.php?section=ebooks&id=<?php echo $id ?>&action=upload_ebook">
        <fieldset>
        <legend><?php echo $lang_profile['Upload Legend'] ?></legend>
                    
        <div class="inform">
        <br>
        <div><?php echo $lang_profile['Upload Info']; ?></div><br>
        <div><?php echo $lang_profile['File Info']; ?></div><br>
        <div><input type="file" name="file" size="70" maxlength="200" /></div><br>
        <div><?php echo $lang_profile['Desc']; ?></div><br>
        <div><textarea name="desc" rows="4" cols="65"></textarea></div><br>
        <div><input type="submit" name="upload_ebook" value="<?php echo $lang_profile['Upload File']; ?>" /></div>
        </div>
        </fieldset>
        </form>
        </div>
        </div>
        
        
        <?php
        
    }

and here is the php code that tryes to upload the file selected:

else if ($action == 'upload_ebook')
{
    
    $file_name = $_POST['file'];
    $file_desc = $_POST['desc'];
    
    if(is_uploaded_file($_FILES['file_name']['tmp_name']) && !file_exists(PUN_ROOT."uploads/".$_FILES['file_name']['name']))
    {
        move_uploaded_file($_FILES['file_name']['tmp_name'], PUN_ROOT."ebooks/".$_FILES['file_name']['name']);
        redirect($_SERVER['PHP_SELF'], "File uploaded");
    }
}

P.S: You can also look at the form from the website: http://www.niqqa.co.uk, register and look at your profile menu

Re: Adding a upload script into profiles

This should do the trick:

else if ($action == 'upload_ebook')
{
    
    $file_name = PUN_ROOT."ebooks/".$_FILES['file']['name'];
    $file_desc = $_POST['desc'];
    
    if(is_uploaded_file($_FILES['file']['tmp_name']) && !file_exists($file_name))
    {
        move_uploaded_file($_FILES['file_name']['tmp_name'], $file_name);
        redirect($_SERVER['PHP_SELF'], "File uploaded");
    }
}

You might want to ttake a look at this too: http://be2.php.net/manual/en/features.file-upload.php

Re: Adding a upload script into profiles

Nope no use, but ill have a look at the link you gave me

Re: Adding a upload script into profiles

Hmmm:

php.net wrote:

Note:  Be sure your file upload form has attribute enctype="multipart/form-data"  otherwise the file upload will not work.

Re: Adding a upload script into profiles

Could be it.

Re: Adding a upload script into profiles

I tryed making my own upload script from that description about the functions and this is what I got:

//Upload directory
    $upload_dir = PUN_ROOT.'ebooks/';
    //Uploaded file
    $uploadfile = $uploaddir . basename($_FILES['file']['name']);
    
    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) 
    {
        message('File upload was unsuccessfull!');
    } 
    else 
    {
        message('File uploaded successfully!');
    }

Keeps showing me a message that file upload was unsuccessfull sad

Re: Adding a upload script into profiles

$upload_dir isn't the same as $uploaddir -.-
Oh, and I'd find it suprising that move_uploaded_file() returns true if it was unsuccesful

14 (edited by Canibal 2006-07-06 15:53)

Re: Adding a upload script into profiles

Finally got it working big_smile, took about 16 hoursbut got it. Ok i need sleep

Thanks for all your help