Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a form for a non-profit-club, that publish posts + up to 20 images. The image-path will be writen in a custom-field. Images are uploaded. Ok, now i want to rename the pictures or (much better) create a folder. But there is a special: the form uploads 2 styles of images. There are up to 15 images for a wanted person (g) and up to 5 images for the person, who is missing this person (s). So i have to seperate the images at the server. The images are given from several input-fields.

I named the input-fields for "g" like:

<input type="file" name="image_1_gesucht" id="image_1_gesucht">
<input type="file" name="image_2_gesucht" id="image_2_gesucht">
...
<input type="file" name="image_15_gesucht" id="image_15_gesucht">

The field for "s" named like:

<input type="file" name="image_1_suchend" id="image_1_suchend">
<input type="file" name="image_2_suchend" id="image_2_suchend">
...
<input type="file" name="image_5_suchend" id="image_5_suchend">

If i can rename the pictures: I could do that with a prefix like g- or s- and a unique number. For the unique number i can use the post-id.

If i create a folder: Then i need a folder named like the post-id. If i can rename the pictures with the prefix, this one folder with the the post-id is ok. If i can't rename the pictures, i need 2 more subfolders - one for "g" and one for "s".

My existing script looks like the following example. I post this, that u can imagine the existing work. Can anybody tell me how to rename the pictures or to create folders? Sorry, i'm silly enough, that i don't can realise/understand, what the wordpress-codex want to tell me. There are two a little bit simiolar articles at stackexchange too ( Rename image uploads with width in filename and Issues renaming images during upload ), but also here: sorry, can't realise/understand. I relly hope, anybody can help me

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
    $file=$_FILES;
    // Funktion zum Image-Upload, falls sie noch nicht existiert
    if ( ! function_exists( 'wp_handle_upload' )) { 
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    }

    // Variablen für die Bilder
    $overrides = array( 'test_form' => false);
    $image_1_gesucht = wp_handle_upload( $file['image_1_gesucht'], $overrides );
    $image_2_gesucht = wp_handle_upload( $file['image_2_gesucht'], $overrides );

    // Benutzerdefinierfte Felder für die Bilder
    add_post_meta($pid,'image_1_gesucht',$image_1_gesucht['url']);
    add_post_meta($pid,'image_2_gesucht',$image_2_gesucht['url']);
}

<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
    <div class="pro50left">
        <fieldset class="images">
            <label for="images">Bild 1:</label>
            <input type="file" name="image_1_gesucht" id="image_1_gesucht">
        </fieldset>
    </div>
    <div class="pro50left">
        <fieldset class="images">
            <label for="images">Bild 2:</label>
            <input type="file" name="image_2_gesucht" id="image_2_gesucht">
        </fieldset>
    </div>
</form>
share|improve this question
add comment

1 Answer

What the similar posts (wich your found) are suggesting, is adding a filter in your functions.php wich will handle the filenames for you. All uploaded files will be filtered by this function before the upload will be handled by wp_handle_upload.

Add the following lines of code in your functions.php:

function wpse_82741_rename_uploaded_file( $image ) {
    global $post;

    // get the post ID
    $post_id = absint( $_REQUEST['post_id'] );

    // get the image extension
    switch( $image['type'] ) {
        case 'image/jpeg' :
            $extension = 'jpg';
            break;
        case 'image/png' :
            $extension = 'png';
            break;
        case 'image/gif' :
            $extension = 'gif';
            break;
    }

    // set the new image name
    $image['name'] = "$post_id.$extension";
}

add_filter( 'wp_handle_upload_prefilter', 'wpse_82741_rename_uploaded_file', 20 );

And all names will be like {post_id}.{extension}.

For the handling of your upload path, there is a question about this.

Upload path handling goes through wp_upload_dir() that applies upload_dir filter to returned information. You should try filtering it for the duration of your code running and adjusting paths to wanted location.

You could try this by using the code explained on this webpage by adding the following code in your functions.php and edit the configuration:

add_filter('upload_dir', 'wpse_82741_upload_dir');
$upload = wp_upload_dir();
remove_filter('upload_dir', 'wpse_82741_upload_dir');

funcion my_upload_dir($upload) {
    $upload['subdir'] = '/sub-dir-to-use' . $upload['subdir'];
    $upload['path']   = $upload['basedir'] . $upload['subdir'];
    $upload['url']    = $upload['baseurl'] . $upload['subdir'];
    return $upload;
}

It is quite a job to accomplish this, but i'm sure it is possible.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.