0

I have a custom module to create galleries using PLupload to upload multiple files. These files have a ".tmp" extension in my "/tmp" folder.

Here is an extract of my $form_state['values']:

[0] => Array(
    [tmppath] => temporary://p17g31n52jo4fvj31ds01efd189b4.tmp
    [tmpname] => p17g31n52jo4fvj31ds01efd189b4.tmp
    [name] => photo_1.jpg
    [status] => done
)

How do I save the files in my public folder with the good extension (like .jpg)? I've tried using file_save_upload() or file_copy() but without success.

Can anyone help me?

I'm on a local server (wamp) as administrator with all permissions. The folders (tmp and site/default) are writable.

I copy here some extracts of my code.

hook_form() :

$form['create']['photos'] = array(
    '#type' => 'plupload',
    '#title' => 'Photos',
    '#upload_validators' => array(
        'file_validate_extensions' => array('jpg jpeg gif png'),
    ),
    '#plupload_settings' => array(
        'runtimes' => 'html5',
        'chunk_size' => '1mb',
    ),
);

hook_form_validate() :

$file = file_save_upload('photos', array(
    'file_validate_is_image' => array(),
    'file_validate_extensions' => array('png gif jpg jpeg'),
));

if ($file) {
    if ($file = file_move($file, 'public://')) {
        $form_state['storage']['file'] = $file;
    } else {
        form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
    }
} else {
    form_set_error('file', t('No file was uploaded.'));
}

It returns "No file was uploaded." I don't understand what I should put as first arguments to the file_save_upload function.

  • File uploading can depend heavily on system information. Can you add a description of the server, your file paths, permissions etc? Also, you should describe the error output you get; there are lots of (wrong) ways to use file_save_upload() and file_copy(). – Seth Battin Jan 4 '13 at 16:48
  • Hi Seth ans thank you for your interest. I have edited my post with several additional informations... – MaudeH Jan 5 '13 at 1:11
1

I finally manage to do what I want...

I didn't understand how to use file_copy ans its $destination argument. We can put a file path, not only a folder path. So, I can save my file.jpg correctly.

My code :

$values = $form_state['values']['create'];
$photos_count = count($values['photos']);
for ($i = 0; $i < $photos_count; $i++) {
    $path = $values['photos'][$i]['tmppath'];
    $name = $values['photos'][$i]['name'];
    // set file datas as an object to pass it in file_copy()
    $file = (object)array(
        'uid' => 0,
        'uri' => $path,
        'filemime' => file_get_mimetype($path),
        'status' => 1
    );
    // copy the file in the correct folder
    if ($file = file_copy($file, $filepath . '/' . $name)) {
        drupal_set_message('Success');
    } else {
        drupal_set_message('Error', 'error');
    }
    // set the field datas for saving the node with an array
    $node->field_album_photos[$node->language][$i] = (array)$file;
}

Have a nice day !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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