0

I am using Drupal 7 and trying to upload a file

function upload_service_menu() {
  $items = array();

  $items['api/v1/upload'] = array(
    'title' => 'File Upload Service',
    'description' => 'Upload files to public directory on this server',
    'page callback' => 'upload_service_job',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Function to get the path, download and store the file on our server.
 */
function upload_service_job() {

    $file = $_FILES;

    $validators = array();
    $file = file_save_upload($file, $validators, 'public://');
    if($file){
        echo "Sucess";
    }else {
        echo "fail";
    }
}

and my local form is

<form action="http://example.com/api/v1/upload/" method="post"
enctype="multipart/form-data">
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

But i am not able to upload file. Seems file_save_upload($file, $validators, 'public://'); function is not working .I checked permissions also.. Can anybody tell me what to do.

Thanks

5
  • "fails" and "does not work" are two of most useless statements in IT. Please describe what exactly happens, how does it look like, what are error messages on both ends of file transmission (Apache error.log, Drupal watchdog, browser's console, whatever applicable). Only with that data we might be able to answer.
    – Mołot
    Aug 27 '13 at 13:43
  • there is no error. as this function is not working $file = file_save_upload($file, $validators, 'public://'); Aug 27 '13 at 13:45
  • How do you know which function fails if there is no error message anywhere?
    – Mołot
    Aug 27 '13 at 13:47
  • $file = file_save_upload($file, $validators, 'public://'); if($file){ echo "Sucess"; }else { echo "fail"; } it prints fails. Aug 27 '13 at 13:48
  • In the source code of file_save_upload there is drupal_set_message before each return FALSE - switch theme to one that can display these messages and post the one that will appear.
    – Mołot
    Aug 27 '13 at 13:51
0

$_FILES is

An associative array of items uploaded to the current script via the HTTP POST method.

file_save_upload() takes as its first parameter

A string specifying the filepath or URI of the uploaded file to save.

Your code essentially does this

$file = file_save_upload($_FILES, $validators, 'public://');

So you're currently passing an array to a function that's expecting a string.

You're probably looking for something along the lines of

$file = file_save_upload($_FILES['file']['name'], $validators, 'public://');

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.