无法在drupal 7中添加图像字段


Unable to add image field in drupal 7

我是drupal的新手,现在我需要以编程方式创建节点。

我可以创建一个简单的节点。但如果我把它和图像场结合起来,它总是失败的。

$file_path = drupal_realpath('tmp/test_image.jpg');
$file = (object) array(
    'uid' => 1,
    'uri' => $file_path,
    'filemime' => file_get_mimetype($file_path),
    'status' => 1,
);
$copy = file_copy($file, 'public://sites/default/files/field/image/testing/', FILE_EXISTS_RENAME);
$node->field_image[LANGUAGE_NONE][0] = (array) $copy;

它总是给我一个错误:(

The specified file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.

我也遇到了同样的问题。在我的案例中,问题是媒体配置"公共文件系统路径"被设置为相对路径sites/default/files。在那之后,我设置了从根开始的绝对路径,所有东西都工作了

我认为,如果您告诉file_copy()存储在"public:"中,则不应该指定完整的路径名。至少,在我看到的例子中,目标看起来更像'public:filename.ext'

正如上面的评论所建议的那样,请检查日志以确定。除了web服务器的错误日志,看看Drupal是否在其报告部分中注意到了任何进一步的细节。

'public://'='sites/default/files'(或指定为默认文件文件夹的任何其他文件夹)。

您的代码:

$copy = file_copy($file, 'public://field/image/testing');

p.S.FILE_EXISTS_RENAME默认为,因此无需定义。

感谢您的输入。我尝试将公共路径"sites/default/files"设置为"testing"或任何其他文件夹,并设置为644。

它起作用(

这是我更新的代码。

$file_path = drupal_realpath('test_image.jpg');
$file = (object) array(
    'uid' => 1,
    'uri' => $file_path,
    'filemime' => file_get_mimetype($file_path),
    'status' => 1,
);
$copy = file_copy($file, 'public://', FILE_EXISTS_RENAME);

我不确定我还需要在网站及其子文件夹中设置什么权限。