Simple file upload

I can’t believe it’s so hard to find a simple file upload script example for WordPress. Everything out there is so involved that it’s hard to follow just to remove the parts I need.

I’m looking for a php script that will allow the user to upload a file, just like the attachment options at the bottom when creating a post to this forum. Just a simple "Choose File" button and an "Upload" button (just 1 file at a time is fine).

Could someone please point me in the right direction?

Thanks.

(sorry about the attachment – I was testing the upload portion, which worked without me pushing the Upload button)

  • erealestatemu
    • Flash Drive

    Thank you Andrew. However, I’m looking for actual code to get this done.

    I created a simple php script outside of WordPress:

    $tmp_name = $_FILES["uploadedfile"]["tmp_name"];

    if (PHP_OS == ‘Linux’:wink:

    $dest = "user-images/";

    else

    $dest = "C:\Internet_Marketing\Projects\localmu\wp-content\user-images\";

    $name = $_FILES["uploadedfile"]["name"];

    $fulldest = $dest.$name;

    move_uploaded_file($tmp_name, $fulldest);

    This works great. However, when I place the same code inside of a WordPress function, the $_FILES["uploadedfile"]["name"] value is NULL. In fact the entire $_FILES variable is NULL. I can’t figure out how to get access to the uploaded file.

  • Andrew
    • Champion of Loops

    Uploading a file with PHP is one thing but uploading a file and inserting it into a post is a bit different.

    You would basically be replicating a lot of existing functionality so I’m afraid that’s not something we can assist with. Best of luck though.

    Thanks,

    Andrew

  • erealestatemu
    • Flash Drive

    I don’t want to insert into a post. I just want the user to be able to specify the image file he wants to use as the background of his post. I’ll then take the file and place it into a shared directory with the blog_id and post_id appended to it and store the filename in the post’s meta. This will allow each page to have a different background image.

    I tried using the code from the login-image plug-in I downloaded from this site as a starting point. While it works fine, my code (which almost identical, but for a few folder names) is not populating the $_FILES variable and I don’t know why. I stepped through the debugger and $_FILES = NULL after the post, while other $_POST are present.

    My function gets called using this hook:

    add_action(‘save_post’, ‘ere_after_save’, 1);

  • erealestatemu
    • Flash Drive

    Basically, what I’m trying to do it give the user some of the same flexibility at the page level they now have at the theme level. I want the user to be able to specify colors, background images, etc. on a per page basis and have the theme read each page’s meta and render the page based on those settings. This will allow them to perform split testing (using Google Analytics for example) on multiple page formats/styles simultaniously without having to switch themes or get multiple blogs as Google can rotate pages automatically for them. This is critical for sales/squeeze pages based sites. Since I charge a monthly fee per blog, I don’t want them to feel pressured into buying multiple blogs just to use different layouts.

    But, back to my basic question: why is the $_FILES array empty by the time I try to use it in the ‘save_post’ action (when values in the $_POST array are still available)? Should I use a different action to access it?

  • Glenn
    • New Recruit

    I just did a quick search through the WPMU code base, and there are several places where the $_FILES array is reset:

    unset($_FILES);

    * wp-admin/includes/media.php

    * wp-admin/media-upload.php

    Perhaps one of these files is being executed during the process and it’s resetting the $_FILES array?

  • erealestatemu
    • Flash Drive

    Ok, so can you recommend a better action to hook my function to? I don’t know how late in the process ‘save_post’ is called, but I couldn’t figure out a better place to put it. What happens when the user pushes the "Update Page" button?

    I was thinking about creating my own mini form and putting an "Upload" button next to the Choose File button, but I don’t know where to send the post to and then have it come back to the same page.