Failed to add image field in drupal 7

Im new to drupal and right now I need to create a node program.

I can create a simple node. But if I included it in the image field, it always failed.

$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; 

It always returns me an error :(

 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. 
+4
source share
4 answers

I had the same problem. In my case, the problem was that the media path "Path to the public file system" was configured as relative path sites / default files / files. After that I set the absolute path from the root, everything worked

+2
source

I think that if you tell file_copy() to be stored in "public", you should not specify the full path name. At least in the examples I saw, the target is more like 'public:filename.ext' .

As follows from the above remark, check your logs to make sure. In addition to the web server error log, see Drupal for more details in the Reports section.

0
source

'public://' = 'sites/default/files' (or any other folder specified as the default folder with files) in the Drupal path.

Your code:

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

PS FILE_EXISTS_RENAME is the default, so there is no need to define it.

0
source

Thanks for your submissions. I tried to set the "sites / default / files" path to the public path to "test" or any other folder and set the value to 644.

It works.: (

Here is my updated code.

 $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); 

I'm not sure what other permissions I need to install on sites and in my subfolders.

0
source

Source: https://habr.com/ru/post/1393686/


All Articles