0

Is there a way to use an arbitrary directory for uploading a file? One that is not the public or private folder? I do not mind if it doesn't save a record in the Database.

$form['upload_logo'] = array(
    '#type' => 'managed_file',
    '#title' => t('Upload a Logo'),
    '#weight' => 18,
    '#upload_location' => drupal_realpath('sites/all/files/images/logos'),
    '#upload_validators'  => array('file_validate_extensions' => array('png gif jpg jpeg')),
);

The current code returns this message: The file could not be uploaded, because the destination "Path Generated By drupal_realpath()" is invalid.

4
  • 1
    Have you tried '#upload_location' => 'file:///path/to/file.ext'?
    – Clive
    Aug 5 '15 at 18:19
  • Good thinking, I did not try that. Got this message: The upload directory file://sites/all/files/images/logos for the file field could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled. When looking at the reports. I am going to check my folder permissions again.
    – Michael E
    Aug 5 '15 at 18:24
  • Try making the path absolute (file:///var/www/...)
    – Clive
    Aug 5 '15 at 19:59
  • Figured it out. I dug into the code a little bit and discovered that it is looking for a valid stream wrapper. The public:// and private:// steam wrappers ship with Drupal while file:/// does not. I created my own by following these instructions and it worked perfectly. drupal.org/node/560424 Thanks again for the tip, could not have figured it out without your lead.
    – Michael E
    Aug 5 '15 at 20:11

Your Answer

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

Browse other questions tagged or ask your own question.